2013-04-28 6 views
1

나는 C#을 웹 서비스에 안드로이드를 사용하여 이미지를 업로드하기 위해 노력하고있어값 <java.lang.String 타입의 XML이 된 JSONObject 안드로이드로 변환 할 수 없습니다

웹 서비스는 이러한 매개 변수를 하지만 매번 앱을 실행합니다!

class ImageUploadTask extends AsyncTask<Void, Void, String> { 
    @Override 
    protected String doInBackground(Void... unsued) { 
     try { 

      HttpClient httpClient = new DefaultHttpClient(); 
      HttpContext localContext = new BasicHttpContext(); 
      HttpPost httpPost = new HttpPost(
        "http://192.168.1.100:8080/ScanFiles.asmx?op=vFileUpload"); 
      MultipartEntity entity = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE); 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bmp.compress(CompressFormat.JPEG, 80, bos); 

      byte[] data = bos.toByteArray(); 

      entity.addPart("fileName", new StringBody(fnameglob)); 
      entity.addPart("data", new ByteArrayBody(data, "image/jpeg", fnameglob)); 

      httpPost.setEntity(entity); 
      HttpResponse response = httpClient.execute(httpPost, 
        localContext); 
      BufferedReader reader = new BufferedReader(
        new InputStreamReader(
          response.getEntity().getContent(), "iso-8859-1")); 

      String sResponse = reader.readLine(); 
      return sResponse; 

     } catch (Exception e) { 
      if (dialog.isShowing()) 
       dialog.dismiss(); 
      Toast.makeText(getApplicationContext(), e.getMessage(), 
        Toast.LENGTH_LONG).show(); 
      Log.e(e.getClass().getName(), e.getMessage(), e); 
      return null; 
     } 

     // (null); 
    } 

어떤 제안 :

Value <?xml of type java.lang.String cannot be converted to JSONObject 

이 코드는 다음과 같습니다 그것은 나에게 오류를 준? 어떤 도움을 매우 인정.

답변

0

는 다음 코드를 사용하여 내 사건을 해결 한 :

protected String doInBackground(Void... unsued) { 
     try { 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bmp.compress(CompressFormat.JPEG, 80, bos); 

      byte[] byte_arr = bos.toByteArray(); 
      String image_str = Base64.encodeBytes(byte_arr); 

      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("data", image_str)); 
      nameValuePairs.add(new BasicNameValuePair("strFileName", 
        fnameglob)); 

      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(
         "http://192.168.1.100:1234/ScanFiles.asmx/vFileUpload"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 

       BufferedReader reader = new BufferedReader(
         new InputStreamReader(response.getEntity() 
           .getContent(), "UTF-8")); 

       String sResponse = reader.readLine(); 
       return sResponse; 

      } catch (Exception e) { 
       Log.e("HTTP", "Error in http connection " + e.toString()); 
      } finally { 

      } 
     } finally { 

     } 
     return "done"; 

     // (null); 
    } 

를하고 base64로 클래스를 찾으려면 당신이 그것을 here

을 찾을 수 있습니다 경우이 웹 서비스 코드

[WebMethod] 
    public void vFileUpload(string data, string strFileName) 
    { 
     try 
     { 
      string str = data.Length.ToString(); 
      string sDestinationScannedFiles = ConfigurationManager.AppSettings["DestinationScannedFiles"].ToString(); 
      //string filePath = Server.MapPath(sDestinationScannedFiles + strFileName); 
      string filePath = sDestinationScannedFiles + strFileName; 

      File.WriteAllBytes(filePath, Convert.FromBase64String(data)); 
     } 

입니다

감사합니다.

관련 문제