gpt4 book ai didi

php - 从 Android 连接到 mySQL 数据库时出错

转载 作者:行者123 更新时间:2023-11-30 23:32:15 30 4
gpt4 key购买 nike

我去过following this tutorial .我一直非常小心地严格按照给定的说明进行操作。我所做的一切都与此人所做的相同,除了在我的 php 文件中我提供了我的主机、用户名、密码和数据库。在我的数据库中,我创建了与教程中完全相同的表(相同的字段类型名称,所有内容。我运行相同的脚本),并且我在 Eclipse 中有完全相同的代码。

这是我的代码。它与给出的相同,但我的 HttpPost 链接是我的。

package com.heatscore.pregame;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class AlertsActivity extends Activity
{
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;


public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// Uses the scores xml layout
setContentView(R.layout.alerts);

String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));

//http post
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://<EXCLUDED FOR QUESTION>/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream 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();
}
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());
}
}
}

不幸的是,当我运行我的应用程序时,LogCat 显示错误:

04-01 00:01:18.483: E/log_tag(5218): Error converting result java.lang.NullPointerException
04-01 00:01:18.483: E/log_tag(5218): Error parsing data org.json.JSONException: End of input at character 0 of

我还向 list 文件添加了正确的权限。是的,该行位于应用程序结束标记之后。

<uses-permission android:name="android.permission.INTERNET"/>

我 ping 主机以确保这不是网络问题。结果是成功的。此外,我在我的手机上运行应用程序,而不是模拟器。程序没有崩溃,但是日志中有错误。

所以如果几乎所有内容都与示例相同,那么为什么我的不起作用?有没有其他人有这个问题,你做了什么?有什么办法可以解决这个问题吗?

最佳答案

想通了! “is”变量 (InputStream) 在不同的 try catch block 中,在下一个 block 中实际上为 null。

以下代码修复了所有问题。请注意,我去掉了一个 try catch,并将代码从它移到了前一个。

希望这对某人有帮助。

package xxx.xxx.xxx;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AlertsActivity extends Activity
{
JSONArray jArray;
String result;
InputStream is;
StringBuilder sb;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.alerts);

String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));

//http post
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxx/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();

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();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection " + 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());
}
}
}

关于php - 从 Android 连接到 mySQL 数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9961127/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com