1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
package lol.siter;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/*
NOTE: You have to give this app permission to access
the internet!!! See the AndroidManifest.xml file
*/
public class NKHttpClient extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText txtUrl = (EditText)findViewById(R.id.url);
final Button btnFetch = (Button)findViewById(R.id.button);
final TextView txtResult = (TextView)findViewById(R.id.content);
btnFetch.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
getRequest(txtResult,txtUrl);
String txt=(String) txtResult.getText();
try {
Toast.makeText(v.getContext(),"2" ,Toast.LENGTH_SHORT).show();
parse(txt);
Toast.makeText(v.getContext(),"3" ,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void getRequest(TextView txtResult, EditText txtUrl){
String url = txtUrl.getText().toString();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try{
HttpResponse response = client.execute(request);
txtResult.setText(HttpHelper.request(response));
}catch(Exception ex){
txtResult.setText("Failed!");
}
}
public void parse(String input) throws Exception {
Toast.makeText(this,"209" ,Toast.LENGTH_SHORT).show();
DocumentBuilderFactory dbFactory = (DocumentBuilderFactory) DocumentBuilderFactory.newInstance();
Toast.makeText(this,"20" ,Toast.LENGTH_SHORT).show();
DocumentBuilder parser =(DocumentBuilder) dbFactory.newDocumentBuilder();
Toast.makeText(this,"21" ,Toast.LENGTH_SHORT).show();
Document document =(Document) parser.parse(new ByteArrayInputStream(input.getBytes()));
Toast.makeText(this,"22" ,Toast.LENGTH_SHORT).show();
Element root = (Element) document.getDocumentElement();
Toast.makeText(this,"23" ,Toast.LENGTH_SHORT).show();
NodeList recipesList = (NodeList)root.getChildNodes();
Toast.makeText(this,"24" ,Toast.LENGTH_SHORT).show();
// Toast.makeText(this,recipesList.getLength() ,Toast.LENGTH_SHORT).show();
for (int i = 0; i < recipesList.getLength(); i++) {
Toast.makeText(this,"25" ,Toast.LENGTH_SHORT).show();
Node recipe =(Node) recipesList.item(i);
Toast.makeText(this,"26" ,Toast.LENGTH_SHORT).show();
NodeList fields =(NodeList) recipe.getChildNodes();
// int x=fields.getLength();
Toast.makeText(this,"27" ,Toast.LENGTH_SHORT).show();
// faute hounni
//Toast.makeText(this,x ,Toast.LENGTH_SHORT).show();
Element hj=(Element) fields.item(0);
Toast.makeText(this,"28" ,Toast.LENGTH_SHORT).show();
String id = (String) hj.getNodeValue();
Toast.makeText(this,"29" ,Toast.LENGTH_SHORT).show();
String title = ((Element) fields.item(1)).getNodeValue();
Toast.makeText(this,"Nadia" ,Toast.LENGTH_SHORT).show();
Toast.makeText(this,title ,Toast.LENGTH_SHORT).show();
}
}
} |
Partager