私はこのフォーラムを長い間利用しています。まず、たくさんの時間を助けてくれてありがとうと言いたいです。答えが見つからないのはこれが初めてで、CRAAZZZYになります...
DBとの間でデータを取得/投稿する非常にシンプルなアプリを作成しようとしています。問題は、データを投稿できないことです(getは問題ありません)。Androidデバイスからデータを取得してDBに送信するphpファイルを使用しています。まず、phpファイルに投稿できません。私はそれをすべて試しました(私が知っていること)。
PHPファイル-
$test=$_POST['v']; print $_POST['v'];
Javaファイル-
public void 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
}
}
Log。*を入力すると、すべて問題がないことがわかります。PHPはデータを取得していないようです...
ASYNTASKの編集-----publicclass Upload extends 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());
}
}
}
主な活動-
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));