2015-04-27 81 views
0

我想上傳一個JSON文件到我使用PHP的服務器。但答覆總是假的,我不知道爲什麼。我試圖切換信息的順序,但它沒有幫助。Android上傳JSON到服務器錯誤

這裏是我的方法的代碼:

public void uploadJSON(){ 
    try { 
     JSONObject toSend = new JSONObject(); 
     toSend.put("user", "guri1"); 
     toSend.put("ts", System.currentTimeMillis()/1000L); 
     toSend.put("lat", 35.45); 
     toSend.put("lon", 48.4); 
     toSend.put("text", "Toto som poslal"); 
     toSend.put("tags", ""); 
     toSend.put("img", ""); 
     JSONTransmitter transmitter = new JSONTransmitter(); 
     transmitter.execute(new JSONObject[] {toSend}); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

JSONTransmitter基本上是一個的AsyncTask。下面是它的代碼:

public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> { 

String url = "MYURLISHERE"; 

@Override 
protected JSONObject doInBackground(JSONObject... data) { 
    JSONObject json = data[0]; 
    HttpClient client = new DefaultHttpClient(); 
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); 

    JSONObject jsonResponse = null; 
    HttpPost post = new HttpPost(url); 
    try { 
     StringEntity se = new StringEntity("json="+json.toString()); 
     post.addHeader("content-type", "application/x-www-form-urlencoded"); 
     post.setEntity(se); 

     HttpResponse response; 
     response = client.execute(post); 
     String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity()); 


     Log.i("Response from server", resFromServer); 
    } catch (Exception e) { e.printStackTrace();} 

    return jsonResponse; 
} 

這裏是PHP:

function postevent(){ 
$data=json_decode(file_get_contents('php://input'), true); 
$ts=$data["ts"]*1; 
$lat=$data["lat"]*1; 
$lon=$data["lon"]*1; 
$user=mysql_real_escape_string($data["user"]); 
$text=mysql_real_escape_string($data["text"]); 
$tags=mysql_real_escape_string($data["tags"]); 
$img=mysql_real_escape_string($data["img"]); 
if ($user!=""){ 
$result=db_query("INSERT INTO events SET ts='$ts',lat='$lat',lon='$lon',user='$user',text='$text',tags='$tags',img='$img'"); 
} else { $result=FALSE; } 
if($result==FALSE){ 
echo "false"; 
} else { 
echo "true"; 
} 
} 
+0

看起來像錯誤發生在服務器端。您需要縮小導致它的範圍:$ user是空的還是db_query失敗? – inmyth

+0

以及我不知道。但是你確定這個錯誤是在服務器端嗎?我該如何調試php腳本?因爲它不是我的服務器 – Traabefi

+0

因爲你從服務器得到迴應,表示「false」。也可能你沒有按照服務器的預期構建你的json。所以基本上你需要調試該服務器來知道它如何處理你的請求。如果你不能修改PHP,那麼你不能調試它。 – inmyth

回答

0

您可以直接訪問服務器(PHP腳本文件的URL),並在請求中設置parems,使不同的調試檢查,它可能幫你。

0

基本上,有一個簡單的錯誤導致了整個問題。我已經使用了StringEntity,所以我把「json =」的東西。而這正是服務器不喜歡的。這裏是工作代碼片段:

@Override 
protected JSONObject doInBackground(JSONObject... data) { 
    JSONObject json = data[0]; 
    HttpClient client = new DefaultHttpClient(); 
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000); 

    JSONObject jsonResponse = null; 
    HttpPost post = new HttpPost(url); 
    try { 
     StringEntity se = new StringEntity(json.toString()); 
     post.addHeader("Content-Type", "application/json"); 
     post.setEntity(se); 

     HttpResponse response; 
     response = client.execute(post); 
     String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity()); 


     Log.i("Response from server", resFromServer); 
    } catch (Exception e) { e.printStackTrace();} 

    return jsonResponse; 
}