我收到错误“方法executeMethod(GetMethod)未定义类型HttpClient”。以下是我的代码。
HttpClient client = new DefaultHttpClient();
GetMethod get = new GetMethod("http://10.0.2.2:8080/GnP22/GetNpostServlet?TicketNo="+etTkt.getText());
try {
int status = client.executeMethod(get);
//TextView 3
//resultado=(TextView)findViewById(R.id.ws_response);
String res="";
if(status!=404)
res=get.getResponseBodyAsString();
else
//res=getString(R.string.ws_not_found);
tvDescription.setText(res);
}
catch (Exception e) {
Log.e("Error:",e.getMessage());
}
finally {
get.releaseConnection();
get=null;
}
我已将httpclient3.1 jar附加为库。
答案 0 :(得分:0)
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet("http://10.0.2.2:8080/GnP22/GetNpostServlet?TicketNo="+etTkt.getText());
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
Log.i("Response",response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
}
} catch (Exception e) {}
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}