使用 HttpResponse 时出现异常 response = client.execute(request);
exception while using HttpResponse response = client.execute(request);
我正在尝试和这个人做同样的事情 here 我正在使用 AsyncTask 调用 executeHttpPost。但是,通过调试应用程序,当它到达
行时
HttpResponse response = client.execute(request);
它跳过所有内容到行
if (in != null) {
我很确定 URL 正在工作。
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}}}}
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}}}}}
logcat:
W/ActivityThread﹕ Application is waiting for the debugger on port 8100...
I/System.out﹕ Sending WAIT chunk
I/art﹕ Debugger is active
Debugger has connected
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ debugger has settled (1496)
I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
I/OpenGLRenderer﹕ Initialized EGL, version 1.4
I/Choreographer﹕ Skipped 429 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 38 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 37 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 703 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 3307 frames! The application may be doing too much work on its main thread.
问题已解决,我添加了权限
<uses-permission android:name="android.permission.INTERNET" />
到AndroidManifest.xml
我正在尝试和这个人做同样的事情 here 我正在使用 AsyncTask 调用 executeHttpPost。但是,通过调试应用程序,当它到达
行时HttpResponse response = client.execute(request);
它跳过所有内容到行
if (in != null) {
我很确定 URL 正在工作。
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}}}}
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}}}}}
logcat:
W/ActivityThread﹕ Application is waiting for the debugger on port 8100...
I/System.out﹕ Sending WAIT chunk
I/art﹕ Debugger is active
Debugger has connected
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ debugger has settled (1496)
I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
I/OpenGLRenderer﹕ Initialized EGL, version 1.4
I/Choreographer﹕ Skipped 429 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 38 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 37 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 703 frames! The application may be doing too much work on its main thread.
I/Choreographer﹕ Skipped 3307 frames! The application may be doing too much work on its main thread.
问题已解决,我添加了权限
<uses-permission android:name="android.permission.INTERNET" />
到AndroidManifest.xml