当用户登录时,仅提取他的数据

时间:2015-10-10 05:57:04

标签: php android mysql json pdo

我是Android开发新手,我正在开展一个项目,我想在登录时只显示用户数据。

这是用户登录后的代码 - :

public class UserProfile extends Activity {
ArrayAdapter<String> adapt ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.list_view);
    adapt=new ArrayAdapter<String>(this,R.layout.list_view,R.id.textView1);
    profile pf = new profile();
    pf.execute();
    ListView lv = (ListView) findViewById(R.id.listview);
    lv.setAdapter(adapt);
    super.onCreate(savedInstanceState);
}




class profile extends AsyncTask<Void, Void, String[]>{
    Dialog pd ;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = ProgressDialog.show(UserProfile.this, "Loading", "Please Wait...");
    }
    @Override
    protected String[] doInBackground(Void... params) {
        List<NameValuePair> l = new ArrayList<NameValuePair>();
        Intent intent = getIntent();
        String user = intent.getStringExtra("user");
        l.add(new BasicNameValuePair("username",user));
        HttpClient hc = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://172.31.147.141/test/json.php");
        StringBuilder sb = new StringBuilder();
        String result = null;
        try {
            post.setEntity(new UrlEncodedFormEntity(l));
            HttpResponse response = hc.execute(post);
            HttpEntity ent = response.getEntity();
            InputStream is = ent.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"),8);

            while((reader.readLine())!=null){
                sb.append(reader.readLine() + "\n");
            }
            result = sb.toString();
        }

        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return jsonfetch(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String[] result) {
        super.onPostExecute(result);
        pd.dismiss();
        for(String ad : result){
            adapt.add(ad);
        }
    }
    private String[] jsonfetch(String result) throws JSONException{

        final String comp_id ="comp_id"; 
        String comp_type="comp_type";
        //String Availabletime="Available_time";
        String stat="stat";
        String Description="Description";
        String address ="address";
        //String name="Name";
        String username="username";
        //String com = "complaint";

        JSONArray ja = new JSONArray(result);
        String[] resultstr = new String[ja.length()];

        for(int i = 0; i<ja.length();i++){
            JSONObject obj = ja.getJSONObject(i);
            int compid = obj.getInt(comp_id);
            String stats = obj.getString(stat);
            String desc=obj.getString(Description);
            String addr=obj.getString(address);
            //String nam = obj.getString(name);
            String user = obj.getString(username);
            String compty=obj.getString(comp_type);
            resultstr[i] = "Comp_id: "+compid+"\nComp_type: "+compty+"\nStats:"+stats+"\nAddress: "+addr+"\nName: "+user+"\nDescription : "+desc;
            }
            return resultstr;

        }
}

}

而且,这是我的Php代码: -

 <?php
if($_SERVER['REQUEST_METHOD']=="POST"){
$username='root';
$password='';
$server='localhost';
try{
$sqlcon = new pdo ("mysql: host=$server;dbname=project ims",$username,$password);
$q1="Select * from f_c_r where username = :user ";

$sql=$sqlcon->prepare($q1);
$sql->bindparam(":user",$_POST['username']);
$sql->execute();
$results=$sql->fetchall();
$json=json_encode($results);
echo $json;
}
catch(PDOEXCEPTION $e){
echo $e;
}
}
?>

当我尝试执行此操作时,logcat显示

  

10-10 12:01:15.700:I / Timeline(28017):时间轴:Activity_idle id:android.os.BinderProxy@16a98ab1 time:68504901   10-10 12:01:16.596:E / SpannableStringBuilder(28017):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度   10-10 12:01:16.597:E / SpannableStringBuilder(28017):SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度   10-10 12:01:19.556:I / Timeline(28017):时间轴:Activity_idle id:android.os.BinderProxy@16a98ab1 time:68508757   10-10 12:01:23.480:I / Timeline(28017):时间轴:Activity_launch_request id:com.complaint time:68512682   10-10 12:01:23.552:D / AndroidRuntime(28017):关闭VM   10-10 12:01:23.554:E / AndroidRuntime(28017):致命异常:主要   10-10 12:01:23.554:E / AndroidRuntime(28017):处理:com.complaint,PID:28017   10-10 12:01:23.554:E / AndroidRuntime(28017):java.lang.RuntimeException:无法启动活动ComponentInfo {com.complaint / com.complaint.UserProfile}:java.lang.NullPointerException:尝试调用虚方法'null android.widget.ListView.setAdapter(android.widget.ListAdapter)'对空对象引用   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.app.ActivityThread.access $ 900(ActivityThread.java:154)   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.os.Handler.dispatchMessage(Handler.java:102)   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.os.Looper.loop(Looper.java:135)   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.app.ActivityThread.main(ActivityThread.java:5289)   10-10 12:01:23.554:E / AndroidRuntime(28017):at java.lang.reflect.Method.invoke(Native Method)   10-10 12:01:23.554:E / AndroidRuntime(28017):at java.lang.reflect.Method.invoke(Method.java:372)   10-10 12:01:23.554:E / AndroidRuntime(28017):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)   10-10 12:01:23.554:E / AndroidRuntime(28017):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)   10-10 12:01:23.554:E / AndroidRuntime(28017):引起:java.lang.NullPointerException:尝试在null上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'对象参考   10-10 12:01:23.554:E / AndroidRuntime(28017):at com.complaint.UserProfile.onCreate(UserProfile.java:42)   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.app.Activity.performCreate(Activity.java:5990)   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)   10-10 12:01:23.554:E / AndroidRuntime(28017):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)   10-10 12:01:23.554:E / AndroidRuntime(28017):......还有10个   10-10 12:01:23.614:W / System.err(28017):org.json.JSONException:类型为org.json.JSONObject $ 1的值null无法转换为JSONArray   10-10 12:01:23.614:W / System.err(28017):at org.json.JSON.typeMismatch(JSON.java:111)   10-10 12:01:23.614:W / System.err(28017):at org.json.JSONArray。(JSONArray.java:96)   10-10 12:01:23.614:W / System.err(28017):at org.json.JSONArray。(JSONArray.java:108)   10-10 12:01:23.614:W / System.err(28017):at com.complaint.UserProfile $ profile.jsonfetch(UserProfile.java:114)   10-10 12:01:23.614:W / System.err(28017):at com.complaint.UserProfile $ profile.doInBackground(UserProfile.java:87)   10-10 12:01:23.614:W / System.err(28017):at com.complaint.UserProfile $ profile.doInBackground(UserProfile.java:1)   10-10 12:01:23.614:W / System.err(28017):在android.os.AsyncTask $ 2.call(AsyncTask.java:292)   10-10 12:01:23.614:W / System.err(28017):at java.util.concurrent.FutureTask.run(FutureTask.java:237)   10-10 12:01:23.614:W / System.err(28017):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)   10-10 12:01:23.614:W / System.err(28017):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   10-10 12:01:23.614:W / System.err(28017):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)   10-10 12:01:23.614:W / System.err(28017):at java.lang.Thread.run(Thread.java:818)

0 个答案:

没有答案