import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HttpClientTest extends Activity implements Button.OnClickListener {
/** Called when the activity is first created. */
public TextView accept;
public Button send;
public EditText showEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
accept= (TextView)findViewById(R.id.accept);
send = (Button) findViewById(R.id.send);
showEditText = (EditText) findViewById(R.id.show);
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String urlString = "http://api.showji.com/locating/?m="+showEditText.getText()+"&output=xml";
String dataString = getUrlData(urlString);
accept.setText(dataString);
Log.i("message", "success");
}
public String getUrlData(String url){
String websiteData = null;
try {
DefaultHttpClient client = new DefaultHttpClient();
URI uri = new URI(url);
HttpGet method = new HttpGet(uri);
HttpResponse res = client.execute(method);
InputStream data = res.getEntity().getContent();
websiteData = generateString(data);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return websiteData;
}
public String generateString(InputStream stream) {
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
try {
String cur;
while ((cur = buffer.readLine()) != null) {
sb.append(cur + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
}
注意:AndroidMinafest.xml文件中须加入
<uses-permission android:name="android.permission.INTERNET" />
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HttpClientTest extends Activity implements Button.OnClickListener {
/** Called when the activity is first created. */
public TextView accept;
public Button send;
public EditText showEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
accept= (TextView)findViewById(R.id.accept);
send = (Button) findViewById(R.id.send);
showEditText = (EditText) findViewById(R.id.show);
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String urlString = "http://api.showji.com/locating/?m="+showEditText.getText()+"&output=xml";
String dataString = getUrlData(urlString);
accept.setText(dataString);
Log.i("message", "success");
}
public String getUrlData(String url){
String websiteData = null;
try {
DefaultHttpClient client = new DefaultHttpClient();
URI uri = new URI(url);
HttpGet method = new HttpGet(uri);
HttpResponse res = client.execute(method);
InputStream data = res.getEntity().getContent();
websiteData = generateString(data);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return websiteData;
}
public String generateString(InputStream stream) {
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
try {
String cur;
while ((cur = buffer.readLine()) != null) {
sb.append(cur + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
}
注意:AndroidMinafest.xml文件中须加入
<uses-permission android:name="android.permission.INTERNET" />