• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps use handler in web service

zalani

Lurker
public Handler serviceCallHnadler = new Handler() {
public void handleMessage(Message msg) {
if (calling_flag == 2 ) {
AllData.firstList=null;
AllData.firstList = (SearchResponse[]) msg.obj;
dialog.dismiss();
calling_flag = 0;
Intent i = new Intent(CLASSNAME.this, CLASS_NAME_List.class);
startActivity(i);
}

public void onClick(View v)
{
calling_flag = 2;
new runWebService();
}


class runWebService extends Thread {
ServiceCaller ser = new ServiceCaller();
public runWebService() {
this.start();
}

@Override
public void run() {
try{
super.run();
if(!checkInternetConnection())
{
Toast.makeText(CLASSNAME.this, "No Internet Connection", Toast.LENGTH_LONG).show();
}else
{

if (calling_flag == 2) {
try {
ser.getresult(serviceCallHnadler);
} catch (Exception e) {
e.printStackTrace();
Log.d("get string search result : ", e.toString());
}
}//if condition closed
}//else condition closed
// serviceCallHnadler.sendEmptyMessage(0);
}catch (Exception e) {

e.printStackTrace();
}
}//run method closed

}//class closed


///--------------Method Written in caller class---------
public void getresult(Handler resHandler) {

String urlparam = "search&searchstring=" + AllData.searchdata+"&lid="+ AllData.languagecode;
SearchResponse[] resColl = null;
Message msg = new Message();
try {
String temUrl = URLEncoder.encode(URL1 + urlparam);
String response1 = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL1 + "search"+"&lid="+ AllData.languagecode);

try {

// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
1);
nameValuePairs.add(new BasicNameValuePair("searchstring",
AllData.searchdata));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
response1 = inputStreamToString(response.getEntity()
.getContent());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

JSONObject rootObj = new JSONObject(response1); // rootObj ist jetzt
// ein dict
JSONArray posts = (JSONArray) rootObj.get("posts");
int count = posts.length();
resColl = new SearchResponse[count];
for (int j = 0; j < count; j++) {
JSONObject firstRoute = (JSONObject) posts.getJSONObject(j);
JSONObject legs = (JSONObject) firstRoute.get("post");
String test = (String) legs.get("uid");

resColl[j] = new SearchResponse(legs);
}

} catch (Exception e) {
e.getMessage();
// res
}
msg.obj = resColl;
resHandler.sendMessage(msg);
// return resColl;

}


///--------------SearchResponse.class---------

public class SearchResponse {

private String Uid;
private String UserName;
private String errorMsg = "";



SearchResponse(JSONObject jsobj) {
try {
this.Uid = jsobj.get("uid").toString();
this.UserName = URLDecoder.decode((String) jsobj.get("name"),
"UTF-8");
// this.errorMsg =

} catch (Exception e) {

e.printStackTrace();
}
}

public String getUid() {
return Uid;
}

public void setUid(String Uid) {
this.Uid = Uid;
}

public String getUserName() {
return UserName;
}

public void setUserName(String UserName) {
this.UserName = UserName;
}
public String getErrorMsg() {
return errorMsg;
}

public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}

}
 
Back
Top Bottom