0
由於HttpClient的是棄用API級別22和API等級23去掉,我根據這個鏈接中使用HttpClient的不工作
android {
useLibrary 'org.apache.http.legacy'
}
和
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'`
:Lik1和Link 2 。我用下面的類連接,並從服務器讀取信息:
public class Webservice {
public static String readurl(String url, ArrayList<NameValuePair> params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
if (params != null) {
method.setEntity(new UrlEncodedFormEntity(params));
}
HttpResponse response = client.execute(method);
InputStream stream = response.getEntity().getContent();
String result = inputstreamTOString(stream);
return result;
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
但是當我跑我的應用程序,它已墜毀在String result = Webservice.readurl(url, params);
下面鱈魚:
public void populateFromServer() {
String url = G.server_service;
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("action", "read"));
String result = Webservice.readurl(url, params);
.
.
.
}
有什麼異常? Loggers中的 – SMA
只是指向這一行並說:E/AndroidRuntime: – hasti
如果讀取響應時出現任何錯誤,那麼您可以嘗試使用EntityUtils.toString(response)讀取響應; –