我想从mysql
数据库中获取一些数据。
PHP代码:
<?php
$verbindung = mysql_connect ("",
"", "")
or die ("keine Verbindung möglich.
Benutzername oder Passwort sind falsch");
mysql_select_db("DB1539594")
or die ("Die Datenbank existiert nicht.");
$q=mysql_query("SELECT * FROM status WHERE ID>'".$_REQUEST['ID']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
如果执行文件,则如下所示:
[{"ID":"1","name":"tester","status":"1","reports":"5","lastTime":"2014-02-27 17:48:25"}]
我的大问题是,我得到一个
FATAL EXCEPTION: AsyncTask #1
这是我的代码:
public class PublicCheck extends Activity {
String URL2;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_public_check);
URL2 = "http://domain.com/real.php";
}
public void check() {
DownloadWebPageTask task = new DownloadWebPageTask();
String x;
task.execute(URL2);
//Log.i("Ergebnis", x+ URL2);
}
class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
urls[0]);
try {
HttpResponse response = httpclient.execute(httppost);
result = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
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")+
", status: "+json_data.getInt("status")+
", reports: "+json_data.getInt("reports")
);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
}
我不知道该怎么办。如果您能帮助我,我将非常高兴:)
这些是错误。
最佳答案
尝试这个..
您在全局变量中给出了String URL2;
,然后
再次在onCreate
中给出了String URL2 = "http://domain.com/real.php";
在check
内,您称为DownloadWebPageTask AsyncTask
x = task.execute(URL2).get();
的URL2
为null,然后将给出NPE
只需像String
一样删除onCreate
内部的URL2 = "http://domain.com/real.php";