Json parsing using thread

JSON parsing using thread

Here, I have written a very simple method to parse JSON web service.

step 1
Show progress dialog before start thread.
Step 2
Create simple runnable Thread.
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub                                   
}
}).start();

Step 3
Parse JSON inside the Thread.

content="{"email":"" +"[email protected]"+  ""}";
        Log.d("Login content", content);
        try {
            json = jParser.getJSONFromUrl(new JSONObject(content),
                    url, "POST");
            if(json!=null){
                if(json.has("user_id")){
                    user_id = json.getString("user_id");
                }
                data =(JSONObject)json.getJSONObject("error");
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        handle_login.sendEmptyMessage(0);
step 4
Handler for the current thread.
private final Handler handle_login = new Handler() {
public void handleMessage(Message msg) {
//Stop progress dialog here.
if(json !=null){
if(user_id!=null){
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();}}
};

Note: don’t forget to set INTERNET PERMISSION.

Leave a Reply

Your email address will not be published. Required fields are marked *