我到处搜索但我找不到答案,我怎样才能发出HTTP请求?我尝试了几种可能性,但没有一种可行。我一直都有错误。
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d("Debug:", "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
}
catch(Exception ex)
{
Log.v("Message: ", ex.getMessage());
return "Helemaal niks!";
}
finally {
if (is != null) {
is.close();
}
}
02-27 17:52:58.611 13571-13571 / com.example.app E / AndroidRuntime:FATAL EXCEPTION:main java.lang.IllegalStateException:无法执行活动的方法 在android.view.View $ 1.onClick(View.java:3838) 在android.view.View.performClick(View.java:4475) 在android.view.View $ PerformClick.run(View.java:18786) 在android.os.Handler.handleCallback(Handler.java:730) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:525) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1187) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:0)
如果您只想发出HTTP get请求,则可以使用此代码而不是代码。
try{
String result = null;
HttpClient httpClient = new DefaultHttpClient();
String myUrl = "your url";
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(myUrl);
HttpResponse response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = null;
while ((line = reader.readLine()) != null) {
result += line + "\n";
}
return result;
}catch(Exception ex)
{
Log.v("Message: ", ex.getMessage());
return "Helemaal niks!";
}
答案 1 :(得分:0)
所有webservice调用等应始终在 AsyncTask 中完成。
这有助于它在不让应用程序等待的情况下执行。
以下示例代码可帮助您了解如何制作网络摄像头。
try {
HttpClient httpclient = new DefaultHttpClient();
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 2000);
HttpConnectionParams.setSoTimeout(httpParameters, 2000);
httpclient = new DefaultHttpClient(httpParameters);
HttpGet request = new HttpGet(Constants.CHECK_UPDATE_URL);
String responseString = null;
HttpResponse response = httpclient.execute(request);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
// Logging Server Responded
LogActions.systemActions("Server Responded");
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
//do your tasks here
}
} catch (Exception e) {
Log.e("Exception", "An impossible exception", e);
}