Php JSON Android错误-尝试调用虚拟方法';java.lang.String org.json.JSONObject.getString(java.lang.String)和#x27;关于空对象引用

Php JSON Android错误-尝试调用虚拟方法';java.lang.String org.json.JSONObject.getString(java.lang.String)和#x27;关于空对象引用,php,android,json,Php,Android,Json,我的应用程序正在崩溃,日志显示此错误: 尝试在以下行的空对象引用上调用虚拟方法“java.lang.String org.json.JSONObject.getString(java.lang.String)”: ab = jobj.getString("title"); 我是Android开发的新手。请帮忙 public class MainActivity extends ActionBarActivity { JSONObject jobj = null; ClientServerIn

我的应用程序正在崩溃,日志显示此错误:

尝试在以下行的空对象引用上调用虚拟方法“java.lang.String org.json.JSONObject.getString(java.lang.String)”:

ab = jobj.getString("title");
我是Android开发的新手。请帮忙

public class MainActivity extends ActionBarActivity {

JSONObject jobj = null;
ClientServerInterface clientServerInterface = new ClientServerInterface();
TextView textView;
String ab = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);
    new RetrieveData().execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings)
    {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class RetrieveData extends AsyncTask<String,String,String>
{
    protected String doInBackground(String... arg0) {
        jobj = clientServerInterface.makeHttpRequest("http://**.***.***.**/printresult.php");
        try {
            ab = jobj.getString("title");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ab;
    }

    protected void onPostExecute(String ab){
        textView.setText(ab);
    }
}
}
也许它在我的php文件中

<?php
require_once('connection.php');

$qry = "SELECT * FROM eventdetails";
$result = mysql_query($qry);
$rows = array();
while($r = mysql_fetch_assoc($result)){
$rows[] = $r;
}
echo json_encode($rows);
mysql_close();
?>

我真的被难住了。请帮忙

像这样试试看

public class MainActivity extends ActionBarActivity {

JSONObject jobj = null;
ClientServerInterface clientServerInterface = new ClientServerInterface();
TextView textView;
String ab = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);
    new RetrieveData().execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings)
    {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class RetrieveData extends AsyncTask<String,String,String>
{
    protected String doInBackground(String... arg0) {
        jobj = clientServerInterface.makeHttpRequest("http://**.***.***.**/printresult.php");
        try {
            ab = jobj.getString("title");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return ab;
    }

    protected void onPostExecute(String ab){
        textView.setText(ab);
    }
}
}
class RetrieveData extends AsyncTask<String,String,JSONObject>{
    protected JSONObject doInBackground(String... arg0) {
        JSONObject jobj = clientServerInterface.makeHttpRequest("http://54.164.136.46/printresult.php");
        return jobj;
    }
    protected void onPostExecute(JSONObject jobj){
        try {
            String ab = jobj.getString("title");
            textView.setText(ab);
        } catch (JSONException e) {
        e.printStackTrace();
        }
    }
}
类RetrieveData扩展异步任务{
受保护的JSONObject doInBackground(字符串…arg0){
JSONObject jobj=clientServerInterface.makeHttpRequest(“http://54.164.136.46/printresult.php");
返回jobj;
}
受保护的void onPostExecute(JSONObject jobj){
试一试{
字符串ab=jobj.getString(“标题”);
textView.setText(ab);
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
像这样试试

class RetrieveData extends AsyncTask<String,String,JSONObject>{
    protected JSONObject doInBackground(String... arg0) {
        JSONObject jobj = clientServerInterface.makeHttpRequest("http://54.164.136.46/printresult.php");
        return jobj;
    }
    protected void onPostExecute(JSONObject jobj){
        try {
            String ab = jobj.getString("title");
            textView.setText(ab);
        } catch (JSONException e) {
        e.printStackTrace();
        }
    }
}
类RetrieveData扩展异步任务{
受保护的JSONObject doInBackground(字符串…arg0){
JSONObject jobj=clientServerInterface.makeHttpRequest(“http://54.164.136.46/printresult.php");
返回jobj;
}
受保护的void onPostExecute(JSONObject jobj){
试一试{
字符串ab=jobj.getString(“标题”);
textView.setText(ab);
}捕获(JSONException e){
e、 printStackTrace();
}
}
}

这通常是将jdk版本的更高版本设置为目标的情况。当我们使用JDK1.7作为构建的目标平台时,我就遇到了这种情况。当更改为JDK1.5时,它解决了这个问题。不确定,但它起作用了。还要提到的是,这不是android应用程序特有的,而是一个部署包。不确定它是否相关,但包是相同的java.lang.string。

这通常是将jdk版本的更高版本设置为目标的情况。当我们使用JDK1.7作为构建的目标平台时,我就遇到了这种情况。当更改为JDK1.5时,它解决了这个问题。不确定,但它起作用了。还要提到的是,这不是android应用程序特有的,而是一个部署包。不确定它是否相关,但包是相同的java.lang.string。

我认为造成问题的原因是URL指向格式错误的JSON字符串。我在提供的URL上得到的是:

[{"title":"School Start","pictureURL": ...   }]
前面和结尾的方括号不应该出现在传递给JSONObject构造函数的字符串中

 json = sb.toString();
        try{
            jobj = new JSONObject(json);
        }catch (JSONException e){
            e.printStackTrace();
        }

您需要修剪json的第一个和最后一个字符,因为我认为构造函数正在抛出一个异常,您正在捕获并忽略该异常

我认为造成困难的原因是您的URL指向格式错误的JSON字符串。我在提供的URL上得到的是:

[{"title":"School Start","pictureURL": ...   }]
前面和结尾的方括号不应该出现在传递给JSONObject构造函数的字符串中

 json = sb.toString();
        try{
            jobj = new JSONObject(json);
        }catch (JSONException e){
            e.printStackTrace();
        }

您需要修剪json的第一个和最后一个字符,因为我认为构造函数正在抛出一个异常,您正在捕获并忽略该异常

正如user1023110所指出的,发生错误是因为构造函数抛出异常。在本例中,我给了构造函数一个JSONArray,它给出了一个错误,即它无法转换为JSONObject。首先,我将其添加到ClientServerInterface类的顶部:

static JSONArray jarr = null;
然后,我添加了这两行:

jarr = new JSONArray(json);
jobj = jarr.getJSONObject(0);
我以前有过这样的经历:

jobj = new JSONObject(json);
该方法现在返回JSONArray的第一个JSONObject


谢谢你的帮助

正如user1023110所指出的,发生错误是因为构造函数抛出异常。在本例中,我给了构造函数一个JSONArray,它给出了一个错误,即它无法转换为JSONObject。首先,我将其添加到ClientServerInterface类的顶部:

static JSONArray jarr = null;
然后,我添加了这两行:

jarr = new JSONArray(json);
jobj = jarr.getJSONObject(0);
我以前有过这样的经历:

jobj = new JSONObject(json);
该方法现在返回JSONArray的第一个JSONObject


谢谢你的帮助

Android Studio现在给了我一个错误。它的意思是:Error:(63,30)Error:MainActivity中的doInBackground(String…)RetrieveData无法重写AsyncTask返回类型中的doInBackground(Params…),JSONObject与其中的String不兼容,结果是类型变量:Params extends Object在类AsyncTask中声明Result extends Object在类AsyncTask中声明还有这个:Error:(62,5)Error:MainActivity.RetrieveData不是抽象的,并且不重写AsyncTaskOops中的抽象方法doInBackground(String…)。我错误地键入了您编写的内容。但现在它仍然给我一个相同的原始错误,除了空指针。Android Studio现在给我一个错误。它的意思是:Error:(63,30)Error:MainActivity中的doInBackground(String…)RetrieveData无法重写AsyncTask返回类型中的doInBackground(Params…),JSONObject与其中的String不兼容,结果是类型变量:Params extends Object在类AsyncTask中声明Result extends Object在类AsyncTask中声明还有这个:Error:(62,5)Error:MainActivity.RetrieveData不是抽象的,并且不重写AsyncTaskOops中的抽象方法doInBackground(String…)。我错误地键入了您编写的内容。但是现在它仍然给了我相同的原始错误和空指针异常,直到得到相同的空指针。。。我在File->Other Settings->Default Settings->Compiler->Java Compiler中更改了它,并尝试了使用eclipse的所有版本。那是朱诺还是靛蓝。也检查一下。很久以前就开始做了。如果可能的话,也尝试更换平台。另外,如果可能的话,在更改这些之后,尝试清理工作区并构建againI,我在Android Studio上。我会在EclipseStill上尝试,得到相同的空指针。。。我在File->Other Settings->Default Settings->Compiler->Java Compiler中更改了它,并尝试了使用eclipse的所有版本。那么哪个呢