0
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});

            toSend.put("id", "2151");
        } 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 возвращало в class MainActivity?
// how i can return jsonResponse in class MainActivity?

1 ответ 1

0

Объявите в вашем Activity метод:

public void myMethod(JSONObject  jsonResponse){
    //что-то делаю
}

В JSONTransmitter реализовать метод onPostExecute:

@Override
protected void onPostExecute(JSONObject  jsonResponse) {
     //здесь можно обратиться к Activity
     myMethod(res);
}
6
  • Ругается, пишет TaskResponse cannot be resolved to a type.
    – LazeR
    9 сен 2014 в 13:27
  • TaskResponse - то, что вы передать хотите. В вашем случае замените на JSONObject 9 сен 2014 в 13:33
  • Ок, а как мне передать jsonResponse под этот void?
    – LazeR
    9 сен 2014 в 13:39
  • @LazeR, у вас же уже написано return jsonResponse; 9 сен 2014 в 13:42
  • Так?......
    – LazeR
    9 сен 2014 в 13:47

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.