我正在尝试编写android studio,该程序从SQLserver获取数据,JSON我将其解析为IIS并使用JSON服务进行访问。但是,当我打开程序时,程序中显示的错误是org.json.JSONException:Value java.net.UnknownHostException of type java.lang.String cannot be converted to 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();
}
}
}}
这是我的DataAdapter类的代码
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);
}
}}
这是我的NewData(构造函数)代码
public class NewData {
public String DocumentNo;
public String EmployeeNo;
public String DocumentDate;}
感谢您的帮助..