2012-05-11 11 views
0

JSONを使用してMYSQLからJSONArrayのデータを取得しようとしているクラスです。JSON MYSQLのデータ

それは画面上に2つのトースト:NullPointerEceptionとそれが私がPHPウェブページに持っているすべてを単語ごとに書き込む別の例外とapperars。

public class ConnectMySql extends Activity{ 
InputStream is; 
//List<String> items; 
JSONArray jArray; 
//String[] items; 
StringBuilder sb; 
//ListView list; 
TextView textv; 
    String result; 
//List<NameValuePair> items; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
//list = (ListView) findViewById(R.id.listview); 

textv = (TextView) findViewById(R.id.tvHI); 
setContentView(R.layout.main); 

try{ 
    HttpClient httpsClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost("http://79.114.48.119/data.php"); 

    List<NameValuePair> nameValue = new ArrayList<NameValuePair>(); 
    httpPost.setEntity(new UrlEncodedFormEntity(nameValue)); 
    HttpResponse httpResponse = httpsClient.execute(httpPost); 
    HttpEntity httpEntity = httpResponse.getEntity(); 
    is = httpEntity.getContent(); 
}catch(Exception e){ 
    Log.e("log_tag", "Error in HTTP connection" + e.toString()); 
    Toast.makeText(this, "http connection error: " + e.toString(), Toast.LENGTH_LONG).show(); 
} 
    try{ 
     BufferedReader BufR = new BufferedReader (new InputStreamReader(is, "iso-8859-1"),8); 
     sb = new StringBuilder(); 
     sb.append(BufR.readLine() + "\n"); 
     String line = "0"; 

     while((line = BufR.readLine()) != null){ 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result = sb.toString(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error in convert String" + e.toString()); 
    } 
    int lat; 
    int lon; 
    try{ 
     jArray = new JSONArray(result); 
     Toast.makeText(this, "no data found" + result, Toast.LENGTH_LONG).show(); 
     JSONObject json_data = null; 
     for(int i = 0; i< jArray.length(); i++){ 
      json_data = jArray.getJSONObject(i); 
      lat = json_data.getInt("latitude"); 
      lon = json_data.getInt("longitude"); 

      textv.setText(lat + lon); 

      //Here i use another function whitch uses lat & lon 
     } 
     //setupList(); 
    }catch(JSONException e){ 
     Toast.makeText(this, "no data found", Toast.LENGTH_LONG).show(); 
    }catch(Exception e){ 
     Toast.makeText(this, "error" + e.toString(), Toast.LENGTH_LONG).show(); 
    } 
} 
} 
/*private void setupList() { 

// TODO Auto-generated method stub 
list.setAdapter(new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, items)); 
} 

}*/ 

お手伝いできますか?どうもありがとうございました !あなたのコード内

+0

投稿スタックトレース。 – kosa

答えて

1

いくつかのミス:

1)setContentView()はfindViewById()、またはXMLレイアウトの参照を使用する他の方法の前にコールしなければなりません。

2)あなたは(HTTPリクエストが別のスレッドから実行されなければならないUIスレッドでのネットワーク操作を実行しようとしている -

があなたの場合は、マニフェストのインターネット許可に追加することを忘れないでください、それが例外をスロー) - そうあなたが忘れていたか、忘れていたか

+0

私はsetcontentviewを前に呼び出しましたが、2番目のポイントを教えてください。私はあなたが何を言っているのか分からない。ありがとうございました。 –

+0

私はsetcontentviewを呼び出しましたが、textviewは変わりません。あなたは私に2番の良いことを説明できますか?私は本当にあなたが意味することを理解していない。どうもありがとうございました ! –

+0

誰も知らない:(? –