如何使用android发送arraylist作为请求参数?

时间:2013-04-29 04:46:42

标签: android android-asynctask

我正在尝试使用GestureOverlayView捕获签名并将签名作为请求发送。如果只发送一个签名,这可以正常工作,现在我想要发送多个签名,为此我试图在arraylist中存储签名。是否有其他可能发送签名而不是使用NameValuePair

这是我的代码:

public class MainActivity extends Activity {

String response;
WebView webView;
String loanCode;

ArrayList<String> arrayList = new ArrayList<String>();
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    String url = intent.getExtras().getString("url");
    loanCode = intent.getExtras().getString("loanCode");
    webView = (WebView) findViewById(R.id.pdfDisplay);
    webView.setWebViewClient(new WebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());
    webView.loadUrl("https://docs.google.com/gview?embedded=true&url="+url);
    //webView.loadUrl(url);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}

@SuppressLint("ShowToast")
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.save:

        try {

            GestureOverlayView gestureView = (GestureOverlayView) findViewById(R.id.signaturePad);

            Bitmap bm = Bitmap.createBitmap(gestureView.getGesture().toBitmap(100, 100, 10, Color.BLACK));
            ByteArrayOutputStream stream = new ByteArrayOutputStream(2056);
            //FileOutputStream fos = openFileOutput("signature.png", Context.MODE_PRIVATE);
            bm.compress(Bitmap.CompressFormat.PNG, 100, stream);

            byte[] image_byte = stream.toByteArray(); // stream.toString().getBytes();//Base64.encode(fos.toString().getBytes(), 0);

            String strImage = Base64.encodeToString(image_byte, Base64.DEFAULT);

            if(arrayList.size()<3){
            arrayList.add(strImage);
            }
            String serverUrl = "http://192.168.3.139:8001/ql/acceptCustomerSignature.do";
            //String serverUrl = "http://192.168.3.139:8001/APDWebTest/ImageRetrieve";
            String[] signatureImage = {serverUrl,strImage,loanCode};

            ArrayList params = new ArrayList();
            params.add(serverUrl);
            params.add(arrayList);
            params.add(loanCode);
            System.out.println("String is "+strImage);

            stream.flush();
            stream.close();

          APDWebApp apd = new APDWebApp();
          apd.execute(params);

       } catch (Exception e) {

           Log.v("Gestures", e.getMessage());

           e.printStackTrace();

      }
        System.out.println("Size of array list "+arrayList.size());
        break;

    case R.id.logout:
        Intent logout = new Intent(this, LoginActivity.class);
        startActivity(logout);
        finish();
        break;
    }
    return false;
    }


private class APDWebApp extends AsyncTask<ArrayList, Void, String>{

    String response = null;
    @Override
    protected String doInBackground(ArrayList... params) {

        try{
            ArrayList parameters = params[0];

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(parameters.get(0).toString());

            ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();

            nameValuePair.add(new BasicNameValuePair("signture", (String)parameters.get(1)));
            nameValuePair.add(new BasicNameValuePair("loanCode", (String) parameters.get(2)));

            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
            //httpPost.setHeader(name, value)
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
            System.out.println("Response is "+response);

        }
        catch(Exception ex){

            ex.printStackTrace();

        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT);
        response = result;
        super.onPostExecute(result);



    }


}

我正在尝试在此步骤arrayList.add(strImage);

添加签名给arraylist

将此arraylist传递给AsyncTask类params.add(arrayList);

使用以下步骤将此添加到NameValuePair

ArrayList parameters = params[0]; nameValuePair.add(new BasicNameValuePair("signture", (String)parameters.get(1)));

我不确定这是否只是将arraylist作为请求参数发送的方法。如果还有其他可能性,请告诉我。

1 个答案:

答案 0 :(得分:0)

NameValuePair是使用参数创建HTTP GET请求的标准方法。 您可以更改向AsyncTask发送信息的方式,因为异步任务的参数为Params... params,您可以发送两个String而不是ArrayList

apd.execute(serverUrl, signature, loancode);

然后只需在doInBackground()

中使用它们
HttpPost httpPost = new HttpPost(params.get(0));

ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("signture", params.get(1)));
nameValuePair.add(new BasicNameValuePair("loanCode", params.get(2)));