0

I am trying to pass a JSONObject to a web service using the following code:

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

I have used the following code to upload a JSONObject to the service:

   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();

It says

   Response could not be obtained because of IOException

EDIT:

Sometimes,it works and when it does it says response code 400,I have been told that the URL is correct.

I get the image from the gallery using an Intent,I use the following code to obtain a path from the Uri and convert it to 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;
}
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183

1 Answers1

0
Big update:Response code 400 

400 Bad Request

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications

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

Re- check your url. The status code is 400 which means server din't understand your request.

Similar post on SO

java io ioexception unable to parse response from server geocoder

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256