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

URL を再確認してください。ステータス コードは 400 です。これは、サーバーがリクエストを理解していないことを意味します。

SOに関する同様の投稿

Java io ioexception はサーバー ジオコーダからの応答を解析できません

于 2014-01-04T10:29:23.917 に答える