JSON配列を構築することができました.onClickイベントがあり、このJSONArrayをPOSTを介して配列を処理するphpファイルに送信します。
現時点では、配列が目的地に到着するかどうか、配列が PHP によって正しく解釈されるかどうかはわかりません。私の POST 呼び出し (Android) のコードは次のとおりです。
Button bsave = (Button) findViewById(R.id.button3);
View.OnClickListener eventHandlerx = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
JSONObject j1;
JSONArray jsonarray=new JSONArray();
ListView lst = (ListView) findViewById(R.id.mylistview);
int count = lst.getCount();
for (int i = 0; i < count; i++)
{
ViewGroup row = (ViewGroup) lst.getChildAt(i);
TextView tvId = (TextView) row.findViewById(R.id.fID);
TextView tvNume = (TextView) row.findViewById(R.id.fTitlu);
TextView tvUM = (TextView) row.findViewById(R.id.fUM);
TextView tvPU = (TextView) row.findViewById(R.id.fPU);
TextView tvCant = (TextView) row.findViewById(R.id.fCant);
jsonarray.put("Item");
j1 = new JSONObject();
try {
j1.put("idx", newid);
j1.put("id", tvId.getText());
j1.put("nume", tvNume.getText());
j1.put("um", tvUM.getText());
j1.put("pu", tvPU.getText());
j1.put("cant", tvCant.getText());
} catch (JSONException e) {
e.printStackTrace();
}
jsonarray.put(j1);
}
Log.d("JSON array","Array to send:"+jsonarray.toString());
sendJson( urlx, jsonarray);
}
private void sendJson(final String urlx, final JSONArray jsonarray) {
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
try {
HttpPost post = new HttpPost(urlx);
StringEntity se = new StringEntity( jsonarray.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if(response!=null){
HttpEntity entity = response.getEntity();
if (entity != null) {
Log.e("POST response",EntityUtils.toString(entity));
}else{
Log.e("POST response","NULL "+EntityUtils.toString(entity));
}
}else{
Log.e("POST response","NULL");
}
} catch(Exception e) {
e.printStackTrace();
Log.e("postJSON","Error at POST with json:"+e.toString());
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
};
bsave.setOnClickListener(eventHandlerx);
配列がphpに正常に投稿されたかどうかを確認するにはどうすればよいですか? 私はphpファイルでそれを行うことができると思います...しかし、どうすれば結果を見ることができますか? 何をしても、ログには次のように表示されます。
org.apache.http.conn.EofSensorInputStream@somehexa
だから私は立ち往生しています。いくつかの解決策を教えてください。POSTED 値を返すために php ファイルはどのように見えるべきか、結果を解釈するために私の Android コードはどのように見えるべきか (デバッグ目的で Log のような場所に表示するだけです)
私が使用するphpは次のとおりです。
<?php
ob_start();
var_dump($_POST);
die();
$json = file_get_contents('php://input');
$obj = json_decode($json);
print $obj;
result = $obj;
$page = ob_get_contents();
ob_end_flush();
$fp = fopen("output.txt","w");
fwrite($fp,$page);
fclose($fp);
?>
ただし、ファイルは表示されません...そしてEclipseはログに何も表示しません...
ありがとうございました