知道为什么不发布任何数据?我从PHP页面$_POST
返回的所有内容都是array(0){}
,换句话说,$_POST
中没有任何内容。 getData()
是一个充满数据的字符串。我正在尝试从弃用的DefaultHttpClient
切换。
HttpURLConnection conn = null;
try {
URL u = new URL(url);
conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setChunkedStreamingMode(0);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//conn.setRequestProperty("Content-Type","application/json");
conn.getOutputStream().write(getData().getBytes("UTF-8"));
conn.connect();
InputStream content = new BufferedInputStream(conn.getInputStream());
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
builder.append(s + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
答案 0 :(得分:0)
最好的方法是 - >>
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
protected void onPreExecute() {
super.onPreExecute();
dialogOff.setMessage("Loading Data..");
}
protected Boolean doInBackground(String... urls) {
try {
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
data=new String(data.getBytes("ISO-8859-1"), "UTF-8");
JSONArray jarray = new JSONArray(data);
LatLng point;
for (int i = 0; i < jarray.length(); i++)
JSONObject object = jarray.getJSONObject(i);
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
if(result)
Toast.makeText(getApplicationContext(), "Data transfered Successfully", Toast.LENGTH_LONG).show();
else{
Toast.makeText(getApplicationContext(), "Unable to fetch from server", Toast.LENGTH_LONG).show();
dialogOff.cancel();
}
}
}