title: How to upload the image to server using json link in android?
body: <p>i am trying to upload the image to server(POST method) by get the image from gallery,but its not upload the image to server.</p>
  
  <p><strong>My Code</strong></p>
  
  <pre><code>browse_image.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
              Intent intent = new Intent();
              intent.setType("image/*");
              intent.setAction(Intent.ACTION_GET_CONTENT);
              startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
  
          }
      });
  
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (resultCode == RESULT_OK) {
          if (requestCode == SELECT_PICTURE) {
              Uri selectedImageUri = data.getData();
  
              selectedImagePath = getPath(selectedImageUri);
              selected_image.setText(""+get_image_name(selectedImageUri));
  
              kb = Double.valueOf(get_image_size(selectedImageUri))/1024;
              mb = kb/1024;
  
              if (mb&gt;1) {
  
                  Toast.makeText(Update_profile.this, "Seleced Image memory is greater than 1 MB", Toast.LENGTH_LONG).show();
              }
          }
      }
  }
  
  public String getPath(Uri uri) {
      // just some safety built in 
      if( uri == null ) {
          // TODO perform some logging or show user feedback
          return null;
      }
  
      String[] projection = { MediaStore.Images.Media.DATA };
      Cursor cursor = managedQuery(uri, projection, null, null, null);
      if( cursor != null ){
  
          int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
          cursor.moveToFirst();
  
          return cursor.getString(column_index);
      }
  
      return uri.getPath();
  }
  </code></pre>
  
  <p><strong>Upload image</strong></p>
  
  <pre><code> class update_profile extends AsyncTask&lt;String, Void, String&gt;{
  
      String viewbranchSuccess;
      String status,userID,username,hash;
  
      protected void onPreExecute() {
  
          progressDialog = new Dialog(Update_profile.this);
          progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
          progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
          progressDialog.setContentView(R.layout.progress_dialog);
          progressDialog.setCancelable(true);
          progressDialog.show();
      }
  
      protected String doInBackground(String... params) {
  
          Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();  
          bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
          byte[] b = baos.toByteArray(); 
          encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
  
          Log.v("selected path", ""+selectedImagePath);
  
  
              try {
  
                  postParameters = new ArrayList&lt;NameValuePair&gt;();
                  postParameters.add(new BasicNameValuePair("hash", Constants.hash));
                  postParameters.add(new BasicNameValuePair("avatar",encodedImage));
  
                  String response = null;
                  try {
  
                  response = CustomHttpClient.executeHttpPost("http://www.abc.com/api/editprofile.json",postParameters);
                  String result = response.toString();
  
                  Log.v("Result", ""+result);
  
                  json_length = new JSONObject(result);
  
                  jsonarray = new JSONArray("["+result+"]");
  
  
                  } catch (Exception e) {
                      e.printStackTrace();
              }
  
          } catch (Exception e) {
              e.printStackTrace();
              progressDialog.dismiss();
      }       
  return null;
  }
  
      protected void onPostExecute(String result) {
  
          progressDialog.dismiss();
  
      }
  }
  </code></pre>
  
  <p><strong>Custom HTTP Client</strong></p>
  
  <pre><code>public class CustomHttpClient {
  public static final int HTTP_TIMEOUT = 30 * 1000; 
  private static HttpClient mHttpClient;
  private static HttpClient getHttpClient() {
      if (mHttpClient == null) {
          mHttpClient = new DefaultHttpClient();
          final HttpParams params = mHttpClient.getParams();
          HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
          HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
          ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
      }
      return mHttpClient;
  }
  public static String executeHttpPost(String url, ArrayList&lt;NameValuePair&gt; postParameters) throws Exception {
      BufferedReader in = null;
      try {
          HttpClient client = getHttpClient();
          HttpPost request = new HttpPost(url);
          UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
          request.setEntity(formEntity);
          HttpResponse response = client.execute(request);
          in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  
          StringBuffer sb = new StringBuffer("");
          String line = "";
          String NL = System.getProperty("line.separator");
          while ((line = in.readLine()) != null) {
              sb.append(line + NL);
          }
          in.close();
  
          String result = sb.toString();
          return result;
      } finally {
          if (in != null) {
              try {
                  in.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  public static String executeHttpGet(String url) throws Exception {
      BufferedReader in = null;
      try {
          HttpClient client = getHttpClient();
          HttpGet request = new HttpGet();
          request.setURI(new URI(url));
          HttpResponse response = client.execute(request);
          in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  
          StringBuffer sb = new StringBuffer("");
          String line = "";
          String NL = System.getProperty("line.separator");
          while ((line = in.readLine()) != null) {
              sb.append(line + NL);
          }
          in.close();
  
          String result = sb.toString();
          return result;
      } finally {
          if (in != null) {
              try {
                  in.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  }
  </code></pre>
  
  <p>I don't know how to solve this issue,can any one know please help me to solve this issue.</p>