请求好友列表的android用户得到一个null


android user requesting friends list gets a null

运行此代码时,我得到一个空点异常。

我试图做的是…将用户电子邮件从应用程序中获取到字符串uemail,并尝试获取与uemail是朋友的朋友。

活性

public class MainActivity extends Activity {
    protected static final String TAG_Name = null;
    int a;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        a = 2;
        Button ref = (Button)findViewById(R.id.button1);
        ref.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                InputStream is = null;
                String result = "";
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                String uemail = String.valueOf(a);
                nameValuePairs.add(new BasicNameValuePair("place_id", uemail));
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(
                            "http://www.hopscriber.com/test.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 stringtry
                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());
                }
                try {
                    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
                    JSONArray jsonArray = new JSONArray(result);
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject object = (JSONObject) jsonArray.get(i);
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put(TAG_Name, object.getString("place_id"));
                            contactList.add(map);
                        }
                    }
                    TextView z = (TextView)findViewById(R.id.textView1);
                    z.setText(TAG_Name);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

php

<?php
include "db_config.php";
$q=mysql_query("SELECT 'name' FROM places WHERE place_id='".$_REQUEST['place_id']."'");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;
print(json_encode($output));
mysql_close();
?>

我似乎找不到答案。。。。请帮帮我。。它使无效

我注意到mysql_query行出现问题,并且以下" WHERE uname='".$_REQUEST['uemail']."'");WHERE子句中未正确转义。

下面是正确的PHP。

 <?php
 $sql=mysql_query("SELECT`uname`,`place`,`time` FROM `check_in` 
 WHERE uname='".$_REQUEST['uemail']."'");
 while($row=mysql_fetch_assoc($sql)) $output[]=$row;
 print(json_encode($output));
 mysql_close();
 ?>

只要您从不调用tryme()(比如在没有预先定义userName的主方法中),您的java看起来就很好。

警告

uname='".$_REQUEST['uemail']."'" 

是一个巨大的SQL注入攻击漏洞。请使用正确的变量转义。

现在,我们在哪里?

在PHP方面,check_in后面有一个",它会产生语法错误。您将没有JSON数组。

如果没有更多的代码或日志,就无法确定NULL指针异常在哪里。

在hopscripter.com.Menu.tryme(Menu.java:224)

检查文件Menu.java第224行中的方法tryme。出现空指针异常

此行有问题。。。

  private void tryme() {
    // TODO Auto-generated method stub
    InputStream is = null;
    String result = "";
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    String uemail = String.valueOf(usernameName);  
    // here the uemail is getting null

uemail的值为NUll,因此发生空指针异常。