我的應用程序崩潰並且日誌顯示此錯誤:JSON Android錯誤 - 嘗試在空對象引用上調用虛擬方法'java.lang.String org.json.JSONObject.getString(java.lang.String)'
嘗試對其中I做線空對象引用調用虛擬方法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);
}
}
}
這裏的其他文件:
public class ClientServerInterface {
static InputStream is = null;
static JSONObject jobj = null;
static String json = "";
public ClientServerInterface(){
}
public JSONObject makeHttpRequest(String url){
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try{
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity httpentity = httpresponse.getEntity();
is = httpentity.getContent();
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
try{
while((line = reader.readLine())!= null){
sb.append(line+"\n");
}
is.close();
json = sb.toString();
try{
jobj = new JSONObject(json);
}catch (JSONException e){
e.printStackTrace();
}
}catch(IOException e){
e.printStackTrace();
}
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
return jobj;
}
}
也許是在我的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();
?>
我真的難住了。請幫忙!
Android Studio現在給我一個錯誤。它說:錯誤:(63,30)錯誤:MainActivity.RetrieveData中的doInBackground(String ...)無法覆蓋doInBackground(Params ...)中的AsyncTask 返回類型的JSONObject不與字符串 其中PARAMS,結果是類型變量兼容: PARAMS延伸類的AsyncTask聲明的對象類的AsyncTask 結果擴展對象聲明 –
而且這一個:錯誤:(62,5)錯誤:MainActivity.RetrieveData不是抽象的,也不重寫抽象方法doInBackground(String ...)在AsyncTask –
對不起,我更新了我的答案現在試試。 – Baniares