我正在使用這個論壇很長一段時間,首先,我想對thanx幫助 很多次。 這是我第一次,我沒有找到答案,這使我CRAAZZZY ...從Android發佈數據到PHP
我試圖寫一個超級簡單的應用程序,從數據庫中獲取/張貼數據。 問題是,我無法發佈數據(得到罰款)。我使用的是一個php文件,它應該從Android設備獲取數據,並將其發送到數據庫。 首先,我不能將它發佈到我的php文件。我試過了(我知道的)。
的PHP文件 -
$test=$_POST['v']; print $_POST['v'];
Java的文件 -
公共無效POSTDATA()拋出ClientProtocolException,IOException異常{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost("https://www.myweb.coom/test.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("v", "123"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
httpclient.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
當我把登錄。*它讓我發現, everthing是可以的。似乎PHP沒有得到任何數據...
編輯ASYNTASK ----- 公共類上傳擴展的AsyncTask {
@Override
protected String doInBackground(String... params) {
try {
JSONObject json = new JSONObject();
json.put("UserName", "test2");
json.put("FullName", "1234567");
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
HttpClient client = new DefaultHttpClient(httpParams);
//
String url = "http://my WAMP server/test.php"+
"json={\"UserName\":1,\"FullName\":2}";
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes(
"UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
if (entity != null) {
InputStream instream = entity.getContent();
String result = "gr8 Success";
Log.i("Read from server", result);
}
} catch (Throwable t) {
Log.e("Request failed: " ,"ERR"+ t.toString());
}
}
}
MainActivity-
public void postData(View v) { //starts on button click
if(upload!=null){
if(upload.getStatus()!= AsyncTask.Status.FINISHED){
Log.d("postData","no need to start AsyncTask");
return;
}
}
upload= new Upload();
upload.execute();
Log.i("Async", "Starting...");
}
PHP- $ json = $ _ GET ['json']; $ obj = json_decode($ json);
$posts = array(1);
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
你試圖寫入您的PHP文件動態與網絡?也許你應該把它寫入文件 – 2012-12-27 19:35:09
在模擬器中運行你的代碼,並在wireshark中轉儲流量。這將幫助你解決你的問題。 – rekire
我正在將它寫入文件中。沒有其他的東西... 和我不使用模擬器,我正在使用一個設備。 – AndroAid