您好,因为我在从mysql获取数据时在活动类中使用了Asynctask。但它显示错误仍然可以看到并帮助我做错了什么? 我已检查连接问题他们没问题,但我认为代码中的错误。我没有使用Android开发的经验,请帮助!!
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
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.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Day extends Activity {
String dt="";
String mc="";
String bc="";
ImageView dateimg;
String name;
String date;
String targetmonth;
InputStream is=null;
String result=null;
String line=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.day_lay);
getCurrDate();
new LongOperation().execute();
dateimg = (ImageView) findViewById(R.id.img);
dateimg.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//getCurrDate();
}
});
}
public String getCurrDate()
{
Date cal = Calendar.getInstance().getTime();
dt = cal.toLocaleString();
mc = dt.substring(0, dt.length() - 17);
bc=mc.substring(4);
System.out.println("Date is"+bc);
date=bc;
return dt;
}
private class LongOperation extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
protected void onPreExecute() {
this.progressDialog = ProgressDialog.show(Day.this, ""," Can you just see your roof...");
}
@Override
protected void onPostExecute(Void result) {
TextView txt = (TextView) findViewById(R.id.status_id);
txt.setText("Executed"); // txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("date",date));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.1.2/wp-content/themes/app/day.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
System.out.println("Mainu pata hai"+result);
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
targetmonth=(json_data.getString("targetmonth"));
Toast.makeText(getBaseContext(), "Name : "+targetmonth,
Toast.LENGTH_SHORT).show();
//System.out.println("Mainu pata hai"+name);
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
return null;
}
}
}
//数据库选择
<?php
$host='127.0.0.1';
$uname='root';
$pwd='password';
$db="android";
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$date=$_REQUEST['date'];
$r=mysql_query("select mat from vicks where date='$date'",$con);
while($row=mysql_fetch_array($r))
{
$flag[name]=$row[name];
}
print(json_encode($flag));
mysql_close($con);
?>
// logcat的
05-24 20:41:48.173: I/System.out(691): Date is24
05-24 20:42:01.749: E/pass 1(691): connection success
05-24 20:42:01.969: E/pass 2(691): connection success
05-24 20:42:01.969: I/System.out(691): Mainu pata hai{"name":"ollow the rhythm of your heart and you'll find out where your going"}
05-24 20:42:02.438: E/Fail 3(691): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-24 20:42:02.438: D/AndroidRuntime(691): Shutting down VM
05-24 20:42:02.438: W/dalvikvm(691): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
05-24 20:42:02.498: E/AndroidRuntime(691): FATAL EXCEPTION: main
05-24 20:42:02.498: E/AndroidRuntime(691): java.lang.NullPointerException
05-24 20:42:02.498: E/AndroidRuntime(691): at com.status.what.Day$LongOperation.onPostExecute(Day.java:93)
05-24 20:42:02.498: E/AndroidRuntime(691): at com.status.what.Day$LongOperation.onPostExecute(Day.java:1)
05-24 20:42:02.498: E/AndroidRuntime(691): at android.os.AsyncTask.finish(AsyncTask.java:602)
05-24 20:42:02.498: E/AndroidRuntime(691): at android.os.AsyncTask.access$600(AsyncTask.java:156)
05-24 20:42:02.498: E/AndroidRuntime(691): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
05-24 20:42:02.498: E/AndroidRuntime(691): at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 20:42:02.498: E/AndroidRuntime(691): at android.os.Looper.loop(Looper.java:137)
05-24 20:42:02.498: E/AndroidRuntime(691): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-24 20:42:02.498: E/AndroidRuntime(691): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 20:42:02.498: E/AndroidRuntime(691): at java.lang.reflect.Method.invoke(Method.java:511)
05-24 20:42:02.498: E/AndroidRuntime(691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-24 20:42:02.498: E/AndroidRuntime(691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-24 20:42:02.498: E/AndroidRuntime(691): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
你想在Android代码中得到targetmonth
,这不在你的JSON文件中
请检查此行
targetmonth=(json_data.getString("targetmonth"));
答案 1 :(得分:0)
targetMonth的Jsonfield为null,您在
的代码中使用此null值Toast.makeText(getBaseContext(), "Name : "+targetmonth,
Toast.LENGTH_SHORT).show();
答案 2 :(得分:0)
正如我已经看到您的网络服务代码。你没有在你的json中传递“targetmonth”作为键。你在php端的json中创建了“name”键。所以在android端而不是访问targetmonth访问“名称”
要获得准确答案,请提供您从网络服务
获得的json值