Tuesday, 22 April 2014

Json parsing using thread

Json parsing using thread         

                Here,i have written very simple method to parse json webservice.

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\":\"" +"test@test.com"+  "\"}";
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) {
// TODO Auto-generated catch block
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:dont forget to set INTERNET PERMISSION.














No comments: