2013-04-29 66 views
2

我試圖使用GestureOverlayView來捕獲簽名,並根據請求發送簽名。這工作正常,如果只有一個簽名被髮送,現在我想要發送多個簽名,爲此我試圖在arraylist中存儲簽名。是否有其他可能性發送簽名而不是使用NameValuePair如何使用android發送arraylsit作爲請求參數?

這是我的代碼:

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來的AsyncTask類params.add(arrayList);

添加此使用的NameValuePair以下內容的ArrayList步驟

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

我不確定這是否只是發送arraylist作爲請求參數的方法。請讓我知道是否有其他可能性。

+0

你是什麼意思發送?將我們指向您想要更改的代碼部分。 – tbkn23 2013-04-29 05:18:01

+0

@tbkn編輯我的問題 – 2013-04-29 05:28:07

回答

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))); 
相關問題