Php Web服务总是返回null值
我在玩我找到的代码。我只能让我的数据库返回空json对象。这是我的密码Php Web服务总是返回null值,php,android,web-services,rest,Php,Android,Web Services,Rest,我在玩我找到的代码。我只能让我的数据库返回空json对象。这是我的密码 public class TableTalkSplash extends Activity { private InputStream is; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.o
public class TableTalkSplash extends Activity {
private InputStream is;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = "";
InputStream is = null;
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("row_id","1"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsite.com/myscript.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//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");
}
is.close();
result=sb.toString();
System.out.println(result); // this is printing "null" to the console
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString()); //exception is being caught here
}
}
}
公共类TableTalkSplash扩展活动{
私有输入流是;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
字符串结果=”;
InputStream=null;
//要发送的年度数据
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“行id”,“1”));
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://mywebsite.com/myscript.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
System.out.println(result);//这是向控制台打印“null”
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
JSONArray jArray=新JSONArray(结果);
对于(int i=0;i我猜db连接或查询失败,请尝试以下方法以帮助调试:
<?php
error_reporting(-1);
ini_set("display_errors", 1);
mysql_connect("host","username","password") or die('Could not connect: ' . mysql_error());;
mysql_select_db("mydb");
$query = "SELECT * FROM dinner_info WHERE row_id ='".mysql_real_escape_string($_REQUEST['row_id'])."'"
$q=mysql_query($query);
if (!$q) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
要在PHP中打开错误报告,请将以下内容放在顶部:错误报告(-1);
。谢谢,这是我的问题的一部分,我不知道如何调试脚本语言ha。我将添加它并查看它的说明。在感谢您向我展示如何调试PHP之后,我将其添加到PHP脚本的顶部!我能够解决我的问题。
<?php
error_reporting(-1);
ini_set("display_errors", 1);
mysql_connect("host","username","password") or die('Could not connect: ' . mysql_error());;
mysql_select_db("mydb");
$query = "SELECT * FROM dinner_info WHERE row_id ='".mysql_real_escape_string($_REQUEST['row_id'])."'"
$q=mysql_query($query);
if (!$q) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();