0

我正在尝试使用以下代码将 JSONObject 传递给 Web 服务:

    static final String URL_SERVICE="http://888topup.com/ImageProcess.svc/UploadImage";

我使用以下代码将 JSONObject 上传到服务:

   new Thread()
        {
            public void run()
            {
                Bitmap bmp=BitmapFactory.decodeFile(pathName);
                ByteArrayOutputStream bos=new ByteArrayOutputStream();
                bmp.compress(CompressFormat.JPEG, 100, bos);
                imgData=bos.toByteArray();
                JSONObject obj=ImageWorker.encodeBytes(imgData);
                HttpParams params=new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(params, TIMEOUT_MILLISEC);
                HttpConnectionParams.setSoTimeout(params, TIMEOUT_MILLISEC);
                HttpClient  client=new DefaultHttpClient(params);
                HttpPost post=new HttpPost(URL_SERVICE);
                ArrayList<BasicNameValuePair> postParameters=new ArrayList<BasicNameValuePair>();
                BasicNameValuePair pair=new BasicNameValuePair("Value",obj.toString());
                postParameters.add(pair);
                try {
                    post.setEntity(new UrlEncodedFormEntity(postParameters));
                    HttpResponse response=client.execute(post);
                    Log.d(TAG, "Post method has executed");
                    int status=response.getStatusLine().getStatusCode();
                    if(status==HttpStatus.SC_OK)
                    {
                        Log.d(TAG, "Data passed successfully to web service");
                        Toast.makeText(getBaseContext(),"Image uploaded successfully",Toast.LENGTH_SHORT).show();
                    }
                } catch (UnsupportedEncodingException e) {
                    Log.e(TAG, "Error setting post entity",e);
                } catch (ClientProtocolException e) {
                    Log.e(TAG, "Response could not be obtained because of client protocol exception",e);
                } catch (IOException e) {
                    Log.e(TAG, "Response could not be obtained because of IOException");
                }
            }
        }.start();

它说

   Response could not be obtained because of IOException

编辑:

有时,它有效,当它显示时response code 400,我被告知 URL 是正确的。

我使用 Intent 从图库中获取图像,我使用以下代码从 Uri 获取路径并将其转换为 JSONObject:

 Uri dataUri=data.getData();
 Log.d(TAG,"Data uri: "+dataUri);
 final String pathName=getRealPathFromUri(dataUri);

    public String getRealPathFromUri(Uri uri)
{
    String[] projection={MediaStore.Images.Media.DATA};
    String selection=null;
    String[] selectionArgs=null;
    String sortOrder=null;
    Cursor c=getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
    if(c!=null && c.getCount()>0 && c.moveToFirst())
        return c.getString(c.getColumnIndex(MediaStore.Images.Media.DATA));     
    return null;
}

    public static JSONObject encodeBytes(byte[] data)
{
    JSONObject result=new JSONObject();
    try {
        String conv=Base64.encodeToString(data, Base64.DEFAULT);
        Log.d(TAG, conv);
        result.put("title", "IMG_"+System.currentTimeMillis());
        result.put("data",conv);
        result.put("size",data.length*1024);
    } catch (JSONException e) {
        Log.e(TAG, "Placing image data in JSONObject failed");
    }
    return result;
}
4

1 回答 1

0
Big update:Response code 400 

400 错误请求

由于语法错误,服务器无法理解该请求。客户端不应该在没有修改的情况下重复请求

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

重新检查您的网址。状态码是 400 这意味着服务器不理解您的请求。

SO上的类似帖子

java io ioexception 无法解析来自服务器地理编码器的响应

于 2014-01-04T10:29:23.917 回答