错误JSONException java.lang.String无法转换为JSONArray
我正在尝试编写android studio,该程序从SQLserver获取数据,并将JSON解析为IIS,然后使用JSON服务访问。但是,当我打开程序时,程序中显示的错误是错误JSONException java.lang.String无法转换为JSONArray,java,android,sql,json,Java,Android,Sql,Json,我正在尝试编写android studio,该程序从SQLserver获取数据,并将JSON解析为IIS,然后使用JSON服务访问。但是,当我打开程序时,程序中显示的错误是org.json.JSONException:Value java.net.UnknownHostException类型java.lang.String无法转换为JSONArray 我正在使用recycleview刷新数据,这是我在android studio中的代码。这是我的主要活动 public class MainAct
org.json.JSONException:Value java.net.UnknownHostException类型java.lang.String无法转换为JSONArray
我正在使用recycleview刷新数据,这是我在android studio中的代码。这是我的主要活动
public class MainActivity extends AppCompatActivity {
// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private RecyclerView recyler_view;
private DataAdapter MyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Make call to AsyncTask
new AsyncFetch().execute();
}
private class AsyncFetch extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your json file resides
// Even you can make call to php file which returns json data
url = new URL("http://11misd17c0001/JSONWebAPI/Handler1.ashx"); // <- link JSON nantinya
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");
// setDoOutput to true as we recieve data from json file
conn.setDoOutput(true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
List<NewData> data=new ArrayList<>();
pdLoading.dismiss();
try {
JSONArray jArray = new JSONArray(result);
// Extract data from json and store into ArrayList as class objects
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
NewData datanew = new NewData();
datanew.DocumentNo= json_data.getString("doc_no");
datanew.EmployeeNo= json_data.getString("emp_no");
datanew.DocumentDate= json_data.getString("doc_date");
data.add(datanew);
}
// Setup and Handover data to recyclerview
recyler_view = (RecyclerView)findViewById(R.id.recyler_view);
MyAdapter = new DataAdapter(MainActivity.this, data);
recyler_view.setAdapter(MyAdapter);
recyler_view.setLayoutManager(new LinearLayoutManager(MainActivity.this));
} catch (JSONException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}}
谢谢你的帮助。我想在这里猜一下,但看起来Android无法与主机对话:
http://11misd17c0001
。这将是一个网络问题,您需要处理这个问题(即,尝试在onPostExecute
中打印result的值;我猜它看起来一点也不像JSONArray)。。。消息将来自返回e.toString()代码>当我访问http://11misd17c0001
通过IIS,它可以访问并显示JSON服务。。我该把它放在哪里呢@Gavinharrist您的网络上是哪台服务器,还是您的本地主机?选中IOException,因为这会给您一些提示。仅供参考:使用Android Emulator,您需要将其视为虚拟机(即,它有自己的网络访问权限),因此如果没有正确配置这些资源(即,暴露于网络),它无法访问您电脑上的资源。我将这些资源托管到IIS,以便每个人都可以访问JSON(如果他们与我有相同的网络)。。对于模拟器来说。。如果我在电脑中打开它,我可以访问它吗@Gavinharrisy你可以在模拟器上的浏览器上试试吗?这就是问题所在(在IOException处放置一个断点,或者在log.e(“MyApp”,e.getMessage());
public class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<NewData> data= Collections.emptyList();
NewData current;
int currentPos=0;
// create constructor to innitilize context and data sent from MainActivity
public DataAdapter(Context context, List<NewData> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.display_data, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}
// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Get current position of item in recyclerview to bind data and assign values from list
MyHolder myHolder= (MyHolder) holder;
NewData current=data.get(position);
myHolder.txtdoc.setText(current.DocumentNo);
myHolder.txtdate.setText(current.EmployeeNo);
myHolder.txtno.setText(current.DocumentDate);
}
// return total item from List
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView txtdoc;
TextView txtdate;
TextView txtno;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
txtdoc = (TextView) itemView.findViewById(R.id.txtdoc);
txtdate = (TextView) itemView.findViewById(R.id.txtdate);
txtno = (TextView) itemView.findViewById(R.id.txtno);
}
}}
public class NewData {
public String DocumentNo;
public String EmployeeNo;
public String DocumentDate;}