new Thread(new Runnable() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try{
HttpPost post = new HttpPost(URL);
post.setHeader("Content-type", "application/json");
json.put("username", userName);
json.put("password", password);
StringEntity se = new StringEntity( json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if(response!=null){
String temp = EntityUtils.toString(response.getEntity()); //Get the data in the entity
JSONObject json2 = (JSONObject)new JSONParser().parse(temp);
//JsonObject o = new JsonParser().parse(temp).getAsJsonObject();
Log.v("response", json2.get("state").toString());
}}
catch(Exception e){
e.printStackTrace();
//createDialog("Error", "Cannot Estabilish Connection");
}
}
}).start();
I have this code and this returns the “temp” string. I wanted to parse this string to JSON object and I also added the json-simple 1.1.1.jar to my refferenced libraries. But the classnotfound error and classdeffound errors shows up in log cat. how to resolve this?
Don’t add jar files like that..follow these steps
You should try this:
Removeall references to theJARin your project from Javaproject -> properties -> Java build path -> librariesCreate a
libsfolder if not exist at the root of your project Copy theJARinto the libs folder.If still not running OK. Right click your project > Android Tools > Fix Project Properties
clean your project and run. it will work