1
JSONObjectに問題があります。私はアンドロイドのアプリに自分のウェブサイトからのデータを受信しています、JSONParserはJSONObjectを返します:JSONObject/JSONArrayを文字列配列に変換する際の問題
class GetAsync extends AsyncTask<String, String, JSONObject> {
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = my_url;
@Override
protected JSONObject doInBackground(String... args) {
JSONObject json = null;
try {
HashMap<String, String> params = new HashMap<>();
Log.d("request", "startingGET");
json = jsonParser.makeHttpRequest(LOGIN_URL, "GET", params);
if (json != null) {
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
protected void onPostExecute(JSONObject json) {
// DUNNO WHAT TO WRITE HERE
}
}
JSONParserが返されます:
D/JSON Parser: result: {"success":1,"message":"Scores downloaded properly","scores":[{"place":"1","nick":"testnick","score":"1149","date":"2016-08-13 14:00:07"},{"place":"2","nick":"testnick","score":"741","date":"2016-08-13 11:35:28"}, [...]
しかし、私
はここに私のgetAsyncクラスですpublic JSONObject makeHttpRequest(String url, String method, HashMap<String, String> params) {
sbParams = new StringBuilder();
int i = 0;
for (String key : params.keySet()) {
try {
if (i != 0){
sbParams.append("&");
}
sbParams.append(key).append("=")
.append(URLEncoder.encode(params.get(key), charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i++;
}
if (method.equals("POST")) {
// NOT IMPORTANT, WORKS FINE
} else if(method.equals("GET")) {
if (sbParams.length() != 0) {
url += "?" + sbParams.toString();
}
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Charset", charset);
conn.setConnectTimeout(15000);
conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
try {
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
Log.d("JSON Parser", "result: " + result.toString());
} catch (IOException e) {
e.printStackTrace();
}
try {
jObj = new JSONObject(result.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
}
conn.disconnect();
return jObj;
}
その結果をString [] []、HashMap、ArrayListなどのようにしたい場合は、その結果をランク付けとして後で表示すると便利です。
json = json.optJSONObject("scores");
JSONArray jsonArray = json.optJSONArray("scores");
:私は
doInBackroundかで
onPostExecute(私は同時にの両方で使用していないよ
)次の行のいずれかを使用することができ、「成功」と「メッセージ」の配列を取り除くために、 ここで私は止めました。前述のタイプの1つに変換する方法を1日中検索していました。私はcharでString stream charを繰り返してみることさえしようとしていましたが、いつも問題がありました。私は他に何をしようと考えていません、私を助けてください。
ただ、 'JSONArray jsonArray = json.optJSONArray( "スコア")を使用し、私は両方で、唯一のこれらの行のを使用していないと述べた'ライン –
; '削除' JSON = json.optJSONObject( "スコア")同じ時間。 – kuznikowski
JSONArray jsonArray = jsonObj。getJSONArray( "得点"); – ravthiru