JSONとHTTPクライアントを使用してデータベースのテーブルからデータを取得しようとしています。私が持っているコードは動作しているようですが、何も出力していませんが、クエリの中で何かを意図的に変更すると、エラートーストの出力が出ます。問題は、JSON出力をビュー画面に表示する方法を調べることです。ここでSQLクエリを取得するときにアンドロイドでJSONを使用してビューに出力する方法
は、完全なHTTPコードです:ここで
public void LoadRemote()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://deucalion0.co.uk/getscores.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 string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
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());
}
//paring data
int score;
String username;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
score=json_data.getInt("score");
username=json_data.getString("username");
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
は私のSQLクエリです:
$sql=mysql_query("select score, username from highscores ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
あなたは、私がこの問題について何かアドバイスをいただければ幸いこの作業here
を見ることができます。
ありがとうございました。
public class Viewscores extends Activity{
Button scores;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewscores);
scores = (Button) findViewById(R.id.bViewscores);
scores.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
LoadRemote();
}
});
}
public void LoadRemote()
{
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://deucalion0.co.uk/getscores.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());
}
BufferedReader myReader = new BufferedReader(new InputStreamReader(is));
}
}
あなたの問題が解決すれば...何か答えを受け入れる..] – 5hssba
こんにちはRaju私はまだあなたの提案に取り組んでいます、私はあなたの答えを受け入れることを心配しないでください、私はいつも答えを受け入れる。私はまだそれを受け入れていないので、まだそれを働かせるのが難しいので、私はあなたがすでに私に多くを提供しているので、あなたにもっと多くの援助を求めることではありませんでした。なぜ私はコードを理解していると思いますが、私は今日もそれに取り組んでいきます。多くの感謝! – deucalion0
ok ok ...もし何かエラーが出たら私に知らせてください...私ももっと学び、助けてくれるように。=] – 5hssba