2016-08-02 125 views
0

更新2:确实是这个问题(请参阅下文)我将其从json请求更改为字符串请求以解决问题。只是意味着我将不得不格式化响应字符串来解析出我想要的。使用Android将数据发布到php服务器

更新发现我认为是android volley post json ID and get result back from PHP server的问题。测试并将提供更新

试图发布图片和一些其他数据使用我创建的PHP服务。似乎无法弄清楚为什么这不起作用。想知道是否有人知道我做错了什么。它似乎永远不会通过if(isset($ _ POST [“picture”]))检查。它使用邮递员时正常工作,但Android的日志文件只是打印出没有发布数据。

<?php 
    header('Content-type : bitmap; charset=utf-8'); 

    if(isset($_POST["picture"])){ 

     $encoded_string = $_POST["picture"]; 
     $image_name = $_POST["name"]; 
     $note = $_POST["note"]; 
     $lat = $_POST["latitude"]; 
     $long = $_POST["longitude"]; 
     $warning = $_POST["status"]; 
     if($warning == "true"){ 
      $status = true; 
     }else{ 
      $status = false; 
     } 

     $key = $_POST["device"]; 

     $decoded_string = base64_decode($encoded_string); 
     $path = 'images/'.$image_name; 
     $file = fopen($path, 'wb'); 

     $is_written = fwrite($file, $decoded_string); 

     fclose($file); 

     if($is_written > 0){ 
      $connection = mysqli_connect('localhost', 'admin', 'apple', 'bottom'); 

      $query = " INSERT INTO pictureNote(picture, path, note, latitude, longitude, status, device) values ('$image_name', '$path', '$note', '$lat', '$long', '$status', '$key');"; 

      $result = mysqli_query($connection, $query); 

      if($result){ 
       $return[success] = $result; 
       echo json_encode($return); 
      }else{ 
       $return[success] = $result; 
       echo json_encode($return); 
      } 

      mysqli_close($connection); 
     } 
    }else{ 
     $return[success] = "No post data"; 
     echo json_encode($return); 
    } 
?> 

的Android代码

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.util.Base64; 
import android.util.Log; 
import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.JsonObjectRequest; 
import com.android.volley.toolbox.Volley; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.ByteArrayOutputStream; 
import java.util.HashMap; 
import java.util.Map; 

public class NetworkCalls { 
    private static NetworkCalls singleton; 
    private RequestQueue mRequestQueue; 

    private NetworkCalls() { 
    } 

    public static synchronized NetworkCalls getInstance() { 
     if (singleton == null) { 
      singleton = new NetworkCalls(); 
     } 
     return singleton; 
    } 

    private RequestQueue getRequestQueue() { 
     if (mRequestQueue == null) { 
      mRequestQueue = Volley.newRequestQueue(MainActivity.instance.getApplicationContext()); 
     } 
     return mRequestQueue; 
    } 

    private <T> void addToRequestQueue(Request<T> req) { 
     getRequestQueue().add(req); 
    } 

    public void createPicturePost(PictureNote data) { 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     Bitmap picture = BitmapFactory.decodeFile(data.getPicture(), bmOptions); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     picture.compress(Bitmap.CompressFormat.JPEG, 50, stream); 
     byte[] pictureArray = stream.toByteArray(); 

     String encodedString = Base64.encodeToString(pictureArray, 0); 
     String url = "http://myUrl/picture.php"; 
     final HashMap<String, String> params = new HashMap<>(); 
     params.put("picture", encodedString); 
     params.put("name", "apple"); 
     params.put("note", data.getNote() + ""); 
     params.put("latitude", data.getLatitude() + ""); 
     params.put("longitude", data.getLongitude() + ""); 
     params.put("status", data.getWarning() + ""); 
     params.put("device", StaticVariables.deviceName); 

     JsonObjectRequest jsObj = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params), new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
// 
       try { 
        System.out.println("Server Response: " + response.getString("success")); 
        System.out.println("Server Response: " + response.toString()); 
       }catch(JSONException ex){ 
        Log.e("NetworkCalls parsing ", ex.toString()); 
       } 
       // On Response recieved 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e("NetworkCalls Pic Error", error.toString()); 
      } 
     }){ 
      @Override 
      public Map<String, String> getHeaders(){ 
       HashMap<String, String> headers = new HashMap<>(); 
       headers.put("Content-type", "bitmap; charset=utf-8"); 
       return headers; 
      } 
     }; 
     addToRequestQueue(jsObj); 
    } 
} 
+0

你用'params'做错了。你用'新的JSONObject(params)'作为json发布它们。但你不应该发送json文本,因为php只需要参数。覆盖'getParams()'。 – greenapps

+0

'更新2:这确实是问题'。请不要以更新开始您的帖子。更新属于最后。而且你不需要更新。你可以说,所有的评论都在矿山下面。这是如何完成的。 '(见下文)'?非常不清楚。 – greenapps

回答

0

你这样做不妥params。您将它们张贴为与new JSONObject(params)的json。但你不应该发送json文本,因为php只需要参数。覆盖getParams()

相关问题