Так что я пытаюсь разобрать JSONObject, но я получаю сообщение об ошибке:Ошибка org.json.JSONException разбора данных: Ожидаемая «:» после п
Error parsing data org.json.JSONException: Expected ':' after n at character 5 of {n "24h_avg": 334.22,n "ask": 335.96,n "bid": 335.7,n "last": 335.84,n "timestamp": "Tue, 23 Dec 2014 22:13:55 -0000",n "volume_btc": 30328.82,n "volume_percent": 82.62n}n
я могу получить json Мне нужно с сервера, но он не разбирается по какой-то нечетной причине.
Вот мой MainActivity.java
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONObject;
public class MainActivity extends ActionBarActivity {
public static final String URL = "https://api.bitcoinaverage.com/ticker/global/USD/";
TextView mPriceText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isNetworkAvailable()) {
JSONParse parse = new JSONParse();
parse.execute();
}
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if(networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting price ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJsonFromUrl(URL);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// jokes = json.getJSONArray(TAG_JOKE);
// getting json from url
//JSONObject c = json.getJ();
// store json item
String price = (String) json.get("24h_avg");
// set json data in textview
mPriceText = (TextView) findViewById(R.id.priceTextView);
mPriceText.setText(price);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Вот мой JSON Parser класс
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONObject getJsonFromUrl(String url) {
// Make request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch(IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
// try parsing the string to a JSON object
jObj = new JSONObject(json);
} catch(Exception e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return json string
return jObj;
}
И, наконец, вот что я JSON пытаюсь разобрать.
{
"24h_avg": 333.8,
"ask": 337.79,
"bid": 337.31,
"last": 337.77,
"timestamp": "Tue, 23 Dec 2014 20:06:17 -0000",
"volume_btc": 29261.88,
"volume_percent": 81.98
}
Да, это все. Благодарю. Не могу поверить, что я этого не видел. – fadelakin