My code often with both of the following error. My code, server are ok. after ping server i can get correct ping and nslookup can return server detailes but in Android i get ConnectTimeoutException error and i dont khnow whats problem.
public class JsonService {
private JSONObject json = new JSONObject();
public JsonService(final String username, final String password, final String functionName) throws Exception {
json.put("username", username);
json.put("password", password);
json.put("function_name", functionName);
}
public String request() throws ExecutionException, InterruptedException {
parseJson foo = new parseJson();
FutureTask<String> futureTask = new FutureTask<String>(foo);
new Thread(futureTask).start();
return futureTask.get();
}
private String convertToString(InputStream inputStream) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
try {
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public class parseJson implements Callable<String> {
@Override
public String call() {
String result = null;
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 50000);
HttpResponse response;
try {
HttpPost post = new HttpPost(G.SERVER_REQUEST);
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if (response != null) {
InputStream stream = response.getEntity().getContent();
result = convertToString(stream);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("Error", String.valueOf(e));
}
return result;
}
}
}
0 comments:
Post a Comment