0
嘿,我一直卡在一個問題很長一段時間..我提交android(日食)main1.java,PHP代碼和PHP輸出希望some1可以幫助從android應用程序發送數據到本地服務器sql
PHP OUTPUT
[{"0":"1","id":"1","1":"James","firstName":"James","2":"Smith","lastName":"Smith","3":"111111111","telephone":"111111111","4":"[email protected]","email":"[email protected]"},{"0":"2","id":"2","1":"Jon","firstName":"Jon","2":"Johnson","lastName":"Johnson","3":"222222222","telephone":"222222222","4":"[email protected]","email":"[email protected]"}]
Notice: Undefined index: firstName in C:\xampp\htdocs\food.php on line 14
Notice: Undefined index: lastName in C:\xampp\htdocs\food.php on line 15
Notice: Trying to get property of non-object in C:\xampp\htdocs\food.php on line 20
NULL test
MAIN1> JAVA
**public class Main1 extends Activity {
InputStream is;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = "";
//Connection
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/test.php");
//http post
try{
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "fail0", Toast.LENGTH_SHORT).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
is.close();
result=sb.toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String col_id = json_data.getString("firstName");
Toast.makeText(getApplicationContext(), col_id, Toast.LENGTH_SHORT).show();
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "fail1", Toast.LENGTH_SHORT).show();
}
JSONObject json = new JSONObject();
//Sending data to php
try{
// Add your data
String fname = "john";
String lname = "smith";
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("firstName", fname.trim()));
nameValuePairs.add(new BasicNameValuePair("lastName", lname.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Toast.makeText(getApplicationContext(), "finalpass", Toast.LENGTH_SHORT).show();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
test.php的
<?php
mysql_connect("127.0.0.1","root","password");
mysql_select_db("addressbook");
$sql = mysql_query("SELECT * FROM colleague");
while($row=mysql_fetch_array($sql))
{
$output[]=$row;
}
print(json_encode($output));
$fname = $_POST['firstName'];
$telephone = $_POST['telephone'];
$fnam = (String)$fname;
//$lname = (isset($_POST['lastName'])) ? $_POST['lastName'] : null;
//$email = (isset($_POST['email'])) ? $_POST['email'] : null;
mysql_query("INSERT INTO colleague(firstName, lastName, telephone, email)
VALUES ($fnam, 'Smith', $telephone, '[email protected]')");
mysql_free_result($sql);
mysql_close();
?>
如果我能在php輸出擺脫了錯誤,並得到FNAME印...這將是巨大的
我編輯了我上面的答案。試試看。 – BobMcboberson
注意:嘗試獲取第17行中的C:\ xampp \ htdocs \ food.php中的非對象屬性 NULL test 第17行是$ name = $ json - > {'firstName'}; 謝謝導致prev錯誤消失...但fname仍然爲NULL – user591124
添加檢查以查看null的確切位置。是文件中的空值。或者它是什麼你使用json的方式。 – BobMcboberson