android用户请求好友列表获取null

时间:2012-07-20 04:19:44

标签: php android

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

我试图做的是...从应用程序中获取useremail到字符串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();
?>

我好像找不到答案....请帮帮我..它老虎无效

4 个答案:

答案 0 :(得分:1)

警告

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

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

现在,我们在哪里?

在PHP方面,你有一个“check_in之后发生语法错误。你将没有JSON数组。

如果没有更多代码或日志,则无法确定NULL指针异常的位置。

答案 1 :(得分:1)

  

at hopscriber.com.Menu.tryme(Menu.java:224)

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

答案 2 :(得分:1)

我注意到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看起来就很好。

答案 3 :(得分:0)

此行存在问题......

  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,因此发生空指针异常。