Эй, ребята, я использую android и java и пытаюсь прочитать некоторые данные json в моем коде, но у меня возникла ошибка: введите org. json. JSONObject не может быть преобразован в JSONArray
У меня есть JSON ДАННЫЕ в моем ответе веб-службы, что-то вроде этого:
{
"status_code": 1,
"gstin": "33 AVFC T7665 1TT",
"fetch Time": "2020-04-19T16:54:49.330",
"name": "A INDUSTRIES (I) PRIVATE LIMITED",
"trade name": " INDIA PRIVATE LIMITED",
"registration Date": "2017-07-01",
"center": "RANGE-III",
"state": "SALT LAKE",
"center_cd": "WA07031536",
"state_cd": "WB0856357",
"constitution": "Private Limited Company",
"type": "Regular",
"status": "Active",
"last Update Date": "2018-04-16",
"cancellation Date": null,
"nature": [
"Wholesale Business",
"Others"
],
"pradr": {
"bnm": "ACHS - 7/6412/YR-C",
"st": "A CHECK POST",
"loc": "PALPURAN",
"bno": "A TRADING CO.",
"stcd": "West Bengal",
"flno": "",
"lt": "",
"lg": "",
"pncd": "7534643445",
"ntr": "Wholesale Business, Others"
},
"adadr": []
}
Вот код, реализованный для чтения этого ответа (json ): Я использую asyncTask , и ошибка в моем методе onPostExecute
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
resultTextView = findViewById(R.id.resultTextView);
}
public void getWeather(View view) {
try {
DownloadTask task = new DownloadTask();
String gstin = URLEncoder.encode(editText.getText().toString(), "UTF-8");
task.execute(www.dddd...............................);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Could not find weather :(",Toast.LENGTH_SHORT).show();
}
}
public class DownloadTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Could not find weather :(",Toast.LENGTH_SHORT).show();
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("JSON",s);
try {
JSONObject jsonObject = new JSONObject(s);
String GST = jsonObject.getString("pradr");
Log.i("GST Details", GST);
JSONArray arr = new JSONArray(GST);
String message = "";
for (int i=0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
String main = jsonPart.getString("loc");
String description = jsonPart.getString("bno");
if (!main.equals("") && !description.equals("")) {
message += main + ": " + description + "\r\n";
}
}
if (!message.equals("")) {
resultTextView.setText(message);
} else {
Toast.makeText(getApplicationContext(),"Could not find weather :(",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"Could not find weather :(",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
}```