2

我正在尝试使用捕获签名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);



    }


}

我正在尝试在此步骤将签名添加到 arraylistarrayList.add(strImage);

将此数组列表传递给 AsyncTask 类params.add(arrayList);

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

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

我不确定这是否是将arraylist 作为请求参数发送的唯一方法。请让我知道是否还有其他可能性。

4

1 回答 1

0

NameValuePair是创建GET带参数的 HTTP 请求的标准方法。您可以更改向您发送信息的方式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)));
于 2013-04-29T05:42:05.877 回答