Java 如何将值传递给另一个类

Java 如何将值传递给另一个类,java,android,http,Java,Android,Http,在我的android项目中,我有pm.class,其中 try { HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String ct = till.toString(entity); Log.d("The error", TAG + " respons

在我的android项目中,我有pm.class,其中

try {

            HttpResponse response = httpClient.execute(httpPost);

            HttpEntity entity = response.getEntity();

            String ct = till.toString(entity);
            Log.d("The error", TAG + " response: " + ct);

            if (response.getStatusLine().getStatusCode() != 201) {

                Pickerror error = new Gson().fromJson(content,
    Pickerror.class);
Log.e("Error1",TAG + "  Error is ["+ tf.getMyname() + "], field["
                                + error.getFill() + "], fullmessage: "
                                + error.getMessage());



                throw new Exception("Error is[" + error.getField()
                        + "] - " + error.getMessage());
            }


            return new Gson().fromJson(content, mat.class);

        } catch (IOException ese) {

            Log.e("error2",
                    TAG + " exception error: "
                            + ese.getMessage());
        }

        return null;
    }
以上代码用于连接到服务器

我还有mainActivity.class,其中我有代码:--


我想将pm.class错误(显示在Log.e()中)显示到MainActivity.class的catch部分。我该怎么做?

您的MainActivity.class

List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("url_parameter_key", name));
params.add(new BasicNameValuePair("url_parameter_key", partner));

new NetworkRequest(new CallbackInterface() {

        @Override
        public void onRequestSuccess(JSONObject jsonObject) {
            Toast.makeText(getApplicationContext(),jsonObject,toString(),
                    Toast.LENGTH_LONG).show();
        }

    }, params).execute();
public class NetworkRequest extends AsyncTask<Void, Void, JSONObject> {

public interface CallbackInterface {

    public void onRequestSuccess(JSONObject jsonObject);

}

String rootUrl="your url";
private CallbackInterface callback;


public NetworkRequest(CallbackInterface callbackInterface,
        List<BasicNameValuePair> params,
        ) {
    this.callback = callbackInterface;
    this.rootUrl =rootUrl + "/?" + URLEncodedUtils.format(params, "utf-8");
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected JSONObject doInBackground(Void... params) {

        return networkRequet();
}

private JSONObject networkRequet() {
    String result = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(rootUrl);
        Log.i("url", rootUrl);
        HttpResponse httpResponse = null;
        if (httpGet != null) {
            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(httpEntity);
            Log.i("Result", result);
        }
        return new JSONObject(result);
    } catch (JSONException e) {
        error = ErrorType.JSONPARSER_ERROR;
        e.printStackTrace();
        return null;
    } catch (ClientProtocolException e) {
        error = ErrorType.UNKNOWN_ERROR;
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        error = ErrorType.IO_ERROR;
        e.printStackTrace();
        return null;
    }
}

@Override
protected void onPostExecute(JSONObject result) {
    super.onPostExecute(result);
        callback.onRequestSuccess(result);
}
List params=new ArrayList();
添加(新的BasicNameValuePair(“url_参数_键”,名称));
添加(新的BasicNameValuePair(“url_参数_键”,partner));
新的网络请求(新的回调接口(){
@凌驾
public void onRequestSuccess(JSONObject JSONObject){
Toast.makeText(getApplicationContext(),jsonObject,toString(),
Toast.LENGTH_LONG).show();
}
},params.execute();
你的pm.class

List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("url_parameter_key", name));
params.add(new BasicNameValuePair("url_parameter_key", partner));

new NetworkRequest(new CallbackInterface() {

        @Override
        public void onRequestSuccess(JSONObject jsonObject) {
            Toast.makeText(getApplicationContext(),jsonObject,toString(),
                    Toast.LENGTH_LONG).show();
        }

    }, params).execute();
public class NetworkRequest extends AsyncTask<Void, Void, JSONObject> {

public interface CallbackInterface {

    public void onRequestSuccess(JSONObject jsonObject);

}

String rootUrl="your url";
private CallbackInterface callback;


public NetworkRequest(CallbackInterface callbackInterface,
        List<BasicNameValuePair> params,
        ) {
    this.callback = callbackInterface;
    this.rootUrl =rootUrl + "/?" + URLEncodedUtils.format(params, "utf-8");
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected JSONObject doInBackground(Void... params) {

        return networkRequet();
}

private JSONObject networkRequet() {
    String result = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(rootUrl);
        Log.i("url", rootUrl);
        HttpResponse httpResponse = null;
        if (httpGet != null) {
            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(httpEntity);
            Log.i("Result", result);
        }
        return new JSONObject(result);
    } catch (JSONException e) {
        error = ErrorType.JSONPARSER_ERROR;
        e.printStackTrace();
        return null;
    } catch (ClientProtocolException e) {
        error = ErrorType.UNKNOWN_ERROR;
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        error = ErrorType.IO_ERROR;
        e.printStackTrace();
        return null;
    }
}

@Override
protected void onPostExecute(JSONObject result) {
    super.onPostExecute(result);
        callback.onRequestSuccess(result);
}
公共类NetworkRequest扩展异步任务{
公共接口回调接口{
public void onRequestSuccess(JSONObject JSONObject);
}
String rootUrl=“您的url”;
私有回调接口回调;
公用网络请求(CallbackInterface CallbackInterface,
列出参数,
) {
this.callback=callbackInterface;
this.rootUrl=rootUrl+“/?”+URLEncodedUtils.format(参数,“utf-8”);
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的JSONObject doInBackground(无效…参数){
返回networkRequet();
}
私有JSONObject networkRequet(){
字符串结果=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet-HttpGet=newhttpget(rootUrl);
Log.i(“url”,rootUrl);
HttpResponse HttpResponse=null;
if(httpGet!=null){
httpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
结果=EntityUtils.toString(httpEntity);
Log.i(“结果”,结果);
}
返回新的JSONObject(结果);
}捕获(JSONException e){
error=ErrorType.JSONPARSER\u错误;
e、 printStackTrace();
返回null;
}捕获(客户端协议例外e){
error=ErrorType.UNKNOWN\u错误;
e、 printStackTrace();
返回null;
}捕获(IOE异常){
error=ErrorType.IO\u error;
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPostExecute(JSONObject结果){
super.onPostExecute(结果);
callback.onRequestSuccess(结果);
}

我们还不清楚您要做什么。这些对象之间是如何关联的?您正在使用哪些对象实例,以及您要在这些对象上调用哪些操作?正如David所说,我们还不完全清楚您要完成什么。但是从我的观点来看,我会更改您所使用的方法的声明要将异常从(在pm.class中(注意:Java中的类名应以大写字母开头))更改为“抛出IOException”。然后必须删除catch子句,并可以在mainActivity中添加try/catch子句。确保网络任务在后台线程中运行。