Wednesday, 8 February 2012

How to form the json string in android or java?


import org.json.JSONObject;

public class Login {
private String Username = "username";
private String Password = "password";
private String jsonString;

public String requestForLogin()
{
JSONObject jsonData  =  new JSONObject();
JSONObject jsonobject= new JSONObject();

try{
jsonobject.put("method", "login");
jsonobject.put("username", Username);
jsonobject.put("password",Password);
jsonobject.put("session_id","");
jsonobject.put("dev_type", "android");
jsonData.put("data", jsonobject);
jsonString=jsonData.toString();
}catch(Exception e)
{
e.printStackTrace();
}
return jsonString;
}
}


The json String is like:
{"data":{
"method": login" , "username":"username" , "password":"password" , "session_id":"" , "dev_type":"android"
}
}

Wednesday, 1 February 2012

How to send Json data to Server in android.



In this part we are sending data to the server(which is developed in node.js) as json String .

The response is responseString.


import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.MultipartPostMethod;

public class HttptRequest {

public String requestServerForData(String JSONString)
{

String responseString = null;
 HttpClient client = new HttpClient();
     MultipartPostMethod mPost = new MultipartPostMethod("ServerURL");
     client.setConnectionTimeout(10000);  
     mPost.addParameter("jsonstring", JSONString);
     try {
int statusCode1 = client.executeMethod(mPost);
responseString=mPost.getResponseBodyAsString();
System.out.println("status response=========" + mPost.getResponseBodyAsString());
mPost.releaseConnection();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseString;
}

Java Class Decompiler

visit  http://java.decompiler.free.fr/?q=jdgui


follow the steps regarding this.