如何将值传递给另一个类

时间:2015-03-23 11:25:03

标签: 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 + " 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 ,我有代码: -

 protected Intent doInBackground(String... params) {


                    String tk = null;
                    Bundle data = new Bundle();
                    try {
                        tk = pmm.us(name,partner);

                        data.putString(tk, name);
                        data.putString(tk2,partner);

                    } catch (Exception e) {

///here I want to show

                    }

                    final Intent re = new Intent();
                    re.putExtras(data);
                    return re;
                }

                @Override
                protected void onPostExecute(Intent intent) {
                    if(?????????){

//I want to show the error
                    } else {
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }
            }.execute();
        }

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

1 个答案:

答案 0 :(得分:0)

您的MainActiviy.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();

您的pm.class

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