Android post Base64 String to PHP

前端 未结 5 503
星月不相逢
星月不相逢 2020-11-30 14:57

------ Solution ------
The issue was on our server. It can only handle post requests if we put www in front of our domain name. So that\'s what

相关标签:
5条回答
  • 2020-11-30 15:17

    Try sending your data as below:

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 60000);  //1 min
    HttpConnectionParams.setSoTimeout(httpParams, 60000);          //1 min
    
    HttpClient client = new DefaultHttpClient(httpParams);
    HttpPost post = new HttpPost("http://myURL/uploadImage.php");
    
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("image", image_str));
    
    post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));   // as UTF-8
    HttpResponse response = client.execute(post);
    
    0 讨论(0)
  • 2020-11-30 15:20

    Have you tried reading the raw post data in PHP? Something like

    $data = $xml = file_get_contents('php://input');

    Have a look at this link for more info

    http://www.codediesel.com/php/reading-raw-post-data-in-php/

    0 讨论(0)
  • 2020-11-30 15:23

    just download the Base64.java http://iharder.sourceforge.net/current/java/base64/ & put that file in your project & then

        just replace the 
    
    String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT); 
    

    this with

    String image_str = Base64.encodeBytes(byte_arr);
    

    replace this post.setEntity(new UrlEncodedFormEntity(pairs)); with

      post.setEntity(new UrlEncodedFormEntity(pairs,HTTP.UTF_8));
    

    Also check while importing the Base64 it will import file from your package & then post to server it will post.

    in your php code just change

    change this
    $base = $_POST["image"]; to
    $base = $_REQUEST['image'];

    0 讨论(0)
  • 2020-11-30 15:26

    This is the code for image:

    ByteArrayOutputStream bao = new ByteArrayOutputStream();                
    
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
    byte [] ba = bao.toByteArray();
    String ba1=Base64.encodeBytes(ba);
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("image",ba1));
    

    This is the HTTP code for sending the image to the server:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://servername.com/uploadimage.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));        
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    

    If you find any more difficulties ask me or a check:

    • this similar tutorial for image uploading
    • Base64 example

    In PHP header file changes :

    header('Content-Type: image/jpg; charset=utf-8');
    $base=$_REQUEST['image'];
    
    0 讨论(0)
  • 2020-11-30 15:32

    Is the HTTP-Response-Code you are getting 200? If so, try setting these header-fields:

    "Accept": "text/html"

    "Accept-Language": "en"

    "Content-Length": yourString.length

    "Content-Type": "application/x-www-form-urlencoded"

    0 讨论(0)
提交回复
热议问题