2014-12-30 45 views
0

我的應用程序崩潰並且日誌顯示此錯誤: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(); 
?> 

我真的難住了。請幫忙!

回答

0

正如user1023110指出的那樣,錯誤發生是因爲構造函數拋出異常。在這種情況下,我給了構造函數一個JSONArray,並給出了它無法轉換爲JSONObject的錯誤。首先,我已將此添加到我的ClientServerInterface類的頂部:

static JSONArray jarr = null; 

然後,我將這兩行:

jarr = new JSONArray(json); 
jobj = jarr.getJSONObject(0); 

,我以前有這樣的:

jobj = new JSONObject(json); 

的方法現在返回JSONArray的第一個JSONObject。

感謝您的幫助!

1

嘗試這樣

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(); 
     } 
    } 
} 
+0

Android Studio現在給我一個錯誤。它說:錯誤:(63,30)錯誤:MainActivity.RetrieveData中的doInBackground(String ...)無法覆蓋doInBackground(Params ...)中的AsyncTask 返回類型的JSONObject不與字符串 其中PARAMS,結果是類型變量兼容: PARAMS延伸類的AsyncTask聲明的對象類的AsyncTask 結果擴展對象聲明 –

+0

而且這一個:錯誤:(62,5)錯誤:MainActivity.RetrieveData不是抽象的,也不重寫抽象方法doInBackground(String ...)在AsyncTask –

+0

對不起,我更新了我的答案現在試試。 – Baniares

0

這通常是用高版本的設定爲對象的JDK版本的情況。當我們使用jdk 1.7作爲構建目標平臺時,這發生在我身上。當改爲jdk 1.5時,它解決了這個問題。不知道,但它的工作。還有提到這不是特定於android應用程序,但是一個部署包。不確定其相關但包是相同的java.lang.string。

+0

仍然獲得相同的空指針...我在文件 - >其他設置 - >默認設置 - >編譯器 - > Java編譯器中更改它,並嘗試所有版本你使用的是eclipse,你使用的是 –

+0

。那麼哪個版本是juno或靛藍。檢查一下。很久以前就開始研究它了。如果可能的話,儘量更換平臺。如果可能,也可以在更改後嘗試清理工作區並重新構建 –

+0

我在Android Studio上。我將在Eclipse上嘗試它 –

3

我相信造成困難的是你的URL指向格式不正確的JSON字符串。我得到在所提供的網址是:

[{"title":"School Start","pictureURL": ... }] 

在前面,並在年底的方括號不應該出現在你傳遞給的JSONObject構造這裏的字符串中。

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

您需要修剪json的第一個和最後一個字符,因爲我相信構造函數會拋出一個您正在捕獲和忽略的異常。

+0

這裏是我添加的內容: json.substring(1); json.substring(0,json.length()); 但我仍然得到這個錯誤。但我現在在日誌中發現了其他東西。 「org.json.JSONException:Value [JSON數據來自WEBPAGE]類型org.json.JSONArray無法轉換爲JSONObject」 –

+0

我非常感謝你! –

+0

太好了。你可以檢查我的答案或upvote它?韓國社交協會。 – user1023110

相關問題