Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SE android异步任务 打包my.home.page; 导入org.apache.http.HttpResponse; 导入org.apache.http.client.HttpClient; 导入org.apache.http.client.methods.HttpPost; 导入org.apache.http.entity.StringEntity; 导入org.apache.http.impl.client.DefaultHttpClient; 导入org.apache.http.params.HttpConnectionParams; 导入org.json.JSONException; 导入org.json.JSONObject; 导入android.os.AsyncTask; 导入android.os.Bundle; 导入android.app.Activity; 导入android.util.Log; 导入android.view.Menu; 导入android.widget.Toast; 公共类MainActivity扩展了活动{ 私有异步任务结果; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); 试一试{ JSONObject toSend=新的JSONObject(); toSend.put(“id”,“2151”); JSONTRANSMITER发送器=新的JSONTRANSMITER(); 结果=发送器.execute(新的JSONObject[]{toSend}); }捕获(JSONException e){ e、 printStackTrace(); } } 公共类JSONTransmitter扩展异步任务{ String url=“Some url”; @凌驾 公共JSONObject doInBackground(JSONObject…数据){ JSONObject json=data[0]; HttpClient=new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(),100000); JSONObject jsonResponse=null; HttpPost=新的HttpPost(url); 试一试{ StringEntity se=新的StringEntity(json.toString()); post.addHeader(“内容类型”、“应用程序/json”); 邮政实体(se); HttpResponse响应; 响应=client.execute(post); 字符串resFromServer=org.apache.http.util.EntityUtils.toString(response.getEntity()); jsonResponse=新的JSONObject(resFromServer); Log.i(“来自服务器的响应”,jsonResponse.getString(“EventId”); }catch(异常e){e.printStackTrace();} 返回jsonResponse; } } }_Java_Android_Android Asynctask - Fatal编程技术网

Java SE android异步任务 打包my.home.page; 导入org.apache.http.HttpResponse; 导入org.apache.http.client.HttpClient; 导入org.apache.http.client.methods.HttpPost; 导入org.apache.http.entity.StringEntity; 导入org.apache.http.impl.client.DefaultHttpClient; 导入org.apache.http.params.HttpConnectionParams; 导入org.json.JSONException; 导入org.json.JSONObject; 导入android.os.AsyncTask; 导入android.os.Bundle; 导入android.app.Activity; 导入android.util.Log; 导入android.view.Menu; 导入android.widget.Toast; 公共类MainActivity扩展了活动{ 私有异步任务结果; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); 试一试{ JSONObject toSend=新的JSONObject(); toSend.put(“id”,“2151”); JSONTRANSMITER发送器=新的JSONTRANSMITER(); 结果=发送器.execute(新的JSONObject[]{toSend}); }捕获(JSONException e){ e、 printStackTrace(); } } 公共类JSONTransmitter扩展异步任务{ String url=“Some url”; @凌驾 公共JSONObject doInBackground(JSONObject…数据){ JSONObject json=data[0]; HttpClient=new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(),100000); JSONObject jsonResponse=null; HttpPost=新的HttpPost(url); 试一试{ StringEntity se=新的StringEntity(json.toString()); post.addHeader(“内容类型”、“应用程序/json”); 邮政实体(se); HttpResponse响应; 响应=client.execute(post); 字符串resFromServer=org.apache.http.util.EntityUtils.toString(response.getEntity()); jsonResponse=新的JSONObject(resFromServer); Log.i(“来自服务器的响应”,jsonResponse.getString(“EventId”); }catch(异常e){e.printStackTrace();} 返回jsonResponse; } } }

Java SE android异步任务 打包my.home.page; 导入org.apache.http.HttpResponse; 导入org.apache.http.client.HttpClient; 导入org.apache.http.client.methods.HttpPost; 导入org.apache.http.entity.StringEntity; 导入org.apache.http.impl.client.DefaultHttpClient; 导入org.apache.http.params.HttpConnectionParams; 导入org.json.JSONException; 导入org.json.JSONObject; 导入android.os.AsyncTask; 导入android.os.Bundle; 导入android.app.Activity; 导入android.util.Log; 导入android.view.Menu; 导入android.widget.Toast; 公共类MainActivity扩展了活动{ 私有异步任务结果; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); 试一试{ JSONObject toSend=新的JSONObject(); toSend.put(“id”,“2151”); JSONTRANSMITER发送器=新的JSONTRANSMITER(); 结果=发送器.execute(新的JSONObject[]{toSend}); }捕获(JSONException e){ e、 printStackTrace(); } } 公共类JSONTransmitter扩展异步任务{ String url=“Some url”; @凌驾 公共JSONObject doInBackground(JSONObject…数据){ JSONObject json=data[0]; HttpClient=new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(),100000); JSONObject jsonResponse=null; HttpPost=新的HttpPost(url); 试一试{ StringEntity se=新的StringEntity(json.toString()); post.addHeader(“内容类型”、“应用程序/json”); 邮政实体(se); HttpResponse响应; 响应=client.execute(post); 字符串resFromServer=org.apache.http.util.EntityUtils.toString(response.getEntity()); jsonResponse=新的JSONObject(resFromServer); Log.i(“来自服务器的响应”,jsonResponse.getString(“EventId”); }catch(异常e){e.printStackTrace();} 返回jsonResponse; } } },java,android,android-asynctask,Java,Android,Android Asynctask,如何在MainActivity类中返回jsonResponse? 我尝试: 作废并调用作废 .得到 等 请帮帮我!)//////////////////////////////////////////////////////////////////////////////////////////////////////// 您可以在活动中将jsonResponse声明为类成员,然后不需要返回它 或者,您可以使用AsyncTask的后期执行来使用doinbackgournd的响应 package

如何在MainActivity类中返回jsonResponse? 我尝试: 作废并调用作废 .得到 等
请帮帮我!)////////////////////////////////////////////////////////////////////////////////////////////////////////

您可以在活动中将jsonResponse声明为类成员,然后不需要返回它

或者,您可以使用AsyncTask的后期执行来使用doinbackgournd的响应

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

    }

}
公共类MainActivity扩展活动{
私有异步任务结果;
JSONObject jsonResponse=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
试一试{
JSONObject toSend=新的JSONObject();
toSend.put(“id”,“2151”);
JSONTRANSMITER发送器=新的JSONTRANSMITER();
结果=发送器.execute(新的JSONObject[]{toSend});
}捕获(JSONException e){
e、 printStackTrace();
}
}
公共类JSONTransmitter扩展异步任务{
String url=“Some url”;
@凌驾
公共JSONObject doInBackground(JSONObject…数据){
JSONObject json=data[0];
HttpClient=new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),100000);
JSONObject jsonResponse=null;
HttpPost=新的HttpPost(url);
试一试{
StringEntity se=新的StringEntity(json.toString());
post.addHeader(“内容类型”、“应用程序/json”);
邮政实体(se);
HttpResponse响应;
响应=client.execute(post);
字符串resFromServer=org.apache.http.util.EntityUtils.toString(response.getEntity());
jsonResponse=新的JSONObject(resFromServer);
Log.i(“来自服务器的响应”,jsonResponse.getString(“EventId”);
}catch(异常e){e.printStackTrace();}
返回jsonResponse;
}
@凌驾
受保护的void onPostExecute(JSONObject jsonResponse){
//用jsonResponse做任何你想做的事情
}
}
}

不起作用,应用程序错误我刚刚在代码中添加了onpostexecute,它应该起作用,如何将其发送到MainActivity类?在活动中声明jsonResponse为类成员,然后在doinbackgound中使用相同的对象。在post execute中,您可以调用正在更新UI的方法,或者在您想要使用json响应的地方调用该方法。您能告诉我如何在Toast.makeText中显示jsonResponse吗?
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

    }
}

}