2014-09-09 4 views
-2
package my.home.page; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.HttpConnectionParams; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    private AsyncTask<JSONObject, JSONObject, JSONObject> result; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     try { 
      JSONObject toSend = new JSONObject(); 
      toSend.put("id", "2151"); 

      JSONTransmitter transmitter = new JSONTransmitter(); 
      result=transmitter.execute(new JSONObject[] {toSend}); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 

    public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> { 

     String url = "Some url"; 

     @Override 
     public JSONObject doInBackground(JSONObject... data) { 
      JSONObject json = data[0]; 
      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); 

      JSONObject jsonResponse = null; 
      HttpPost post = new HttpPost(url); 
      try { 
       StringEntity se = new StringEntity(json.toString()); 
       post.addHeader("content-type", "application/json"); 
       post.setEntity(se); 

       HttpResponse response; 
       response = client.execute(post); 
       String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity()); 

       jsonResponse=new JSONObject(resFromServer); 
       Log.i("Response from server", jsonResponse.getString("EventId")); 

      } catch (Exception e) { e.printStackTrace();} 

      return jsonResponse; 
     } 

    } 

} 

Как я могу вернуть jsonResponse в классе MainActivity? Я пытаюсь: void and call void .get etc Pls help me!) ////////////////////////////// ////////////////////////////////////////////////// ////////////////////////Java SE android AsyncTask

ответ

1

Вы можете объявить jsonResponse как члена класса в своей деятельности, тогда вам не нужно его возвращать.

или вы можете использовать после выполнения из AsyncTask использования отклика doinbackgournd

public class MainActivity extends Activity { 

private AsyncTask<JSONObject, JSONObject, JSONObject> result; 
    JSONObject jsonResponse = null; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    try { 
     JSONObject toSend = new JSONObject(); 
     toSend.put("id", "2151"); 

     JSONTransmitter transmitter = new JSONTransmitter(); 
     result=transmitter.execute(new JSONObject[] {toSend}); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

} 

public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> { 

    String url = "Some url"; 

    @Override 
    public JSONObject doInBackground(JSONObject... data) { 
     JSONObject json = data[0]; 
     HttpClient client = new DefaultHttpClient(); 
     HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); 
     JSONObject jsonResponse = null; 
     HttpPost post = new HttpPost(url); 
     try { 
      StringEntity se = new StringEntity(json.toString()); 
      post.addHeader("content-type", "application/json"); 
      post.setEntity(se); 

      HttpResponse response; 
      response = client.execute(post); 
      String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity()); 

      jsonResponse=new JSONObject(resFromServer); 
      Log.i("Response from server", jsonResponse.getString("EventId")); 

     } catch (Exception e) { e.printStackTrace();} 

     return jsonResponse; 
    } 
    @Override 
    protected void onPostExecute(JSONObject jsonResponse) { 
    // do whatever you want to do with jsonResponse 

    } 
} 

} 
+0

не работают, ошибки –

+1

приложения Я просто добавил onpostexecute в своем коде, он должен работать –

+0

Так как отправить его в класс MainActivity? –