Android 使用安卓4.0休息
各位,我的电脑上运行着一个web服务,最近我把我的应用程序从2.2版改成了2.2版。对于4.0,在那之后,我无法再连接到我的WS 我在寻找答案,但什么也没找到 我的应用程序引用的URL如下Android 使用安卓4.0休息,android,rest,Android,Rest,各位,我的电脑上运行着一个web服务,最近我把我的应用程序从2.2版改成了2.2版。对于4.0,在那之后,我无法再连接到我的WS 我在寻找答案,但什么也没找到 我的应用程序引用的URL如下http://10.0.2.2:8080。。。但它不起作用 这是我的密码: private static final String URL_WS = "http://10.0.2.2:8080/WS_TaxiShare/)"; public String login(String email, String
http://10.0.2.2:8080
。。。但它不起作用
这是我的密码:
private static final String URL_WS = "http://10.0.2.2:8080/WS_TaxiShare/)";
public String login(String email, String password) throws Exception {
String[] resposta = new WSClient().get(URL_WS + "login/login/?login="+ email +"&password="+ password);
String saida = resposta[1];
if (resposta[0].equals("200")) {
return saida;
} else {
return saida;
}
}
现在是WSClient
public class WSClient {
public final String[] get(String url) {
String[] result = new String[2];
HttpGet httpget = new HttpGet(url);
HttpResponse response;
try {
Log.i("Get taxi", "Url -> " + url);
response = HttpClientSingleton.getHttpClientInstace().execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
result[0] = String.valueOf(response.getStatusLine().getStatusCode());
InputStream instream = entity.getContent();
result[1] = toString(instream);
instream.close();
Log.i("get", "Result from post JsonPost : " + result[0] + " : " + result[1]);
}
} catch (Exception e) {
Log.i("Exception no get WS taxi", "Exception ->" + e);
result[0] = "0";
result[1] = "Falha de rede!";
}
return result;
}
嗯,我能解决我的问题。Android 4.0(我不知道它什么时候开始),你不能在主线程上调用Web服务。您所需要做的就是创建一个异步方法,在一个单独创建的线程中完成您需要的工作 这是我的方法
private class loginTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
try {
WSTaxiShare ws = new WSTaxiShare();
response = ws.login(login, password);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String strJson) {
}
}
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
loginTask task = new loginTask();
task.execute(new String[] { "" });
}
});