2015-04-23 122 views
-4

我想从json格式的数据发送到PHP的web服务。 但我的web服务没有得到任何回应。 我试了一下:php没有从android获取数据

public void postLatLong (double lat, double lon) throws MalformedURLException { 
    try { 
     URL url = new URL("http://server/path/index.php"); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     OutputStream out = connection.getOutputStream(); 
     OutputStreamWriter outwriter = new OutputStreamWriter(out, "UTF-8"); 
     BufferedWriter writer = new BufferedWriter(outwriter); 
     JSONObject json = new JSONObject(); 
     json.put("lat", lat); 
     json.put("lon", lon); 
     Log.d("JSONString",json.toString()); 
     writer.write(json.toString()); 
     writer.flush(); 
     writer.close(); 
     connection.connect(); 
     InputStream in = connection.getInputStream(); 
     InputStreamReader inreader = new InputStreamReader(in, "UTF-8"); 
     BufferedReader reader = new BufferedReader(inreader); 
     Log.d("Response",reader.readLine()); 
     reader.close(); 
    } catch (IOException e) { 
     Log.d("posting Error", e.getMessage()); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.d("JSONException", e.getMessage()); 
    } 
} 

我收到respons。 没有显示错误日志,json没问题,我可以在日志中看到它。但是,当我检查webservice日志时,POST请求中没有任何内容。

+0

请在您的问题中包含错误消息/堆栈跟踪/事件日志信息。 –

回答

0

入住在PHP代码/配置头设置与否如果没有再设置

header('Content-Type: application/json;charset=UTF-8'); 

它将对您有所帮助。

+0

这不会对我有用。 – PJ1405