我在一家应用咨询公司实习,我被要求制作一个ANDROID应用程序,使用WCF服务从数据库获取/发送一些JSON数据,我所能实现的是与网址连接,但我解析数据时出错(虽然我认为请愿书甚至没有到达数据库)。
我的问题是我不知道是不是因为代码错误或因为我没有正确登录数据库。
它需要用户和密码,因为该应用程序应该从数据库中获取/发送来自特定用户的数据。
我会在这里删除整个代码,并标记我遇到的一些 *
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clientes);
btnpost = (Button)findViewById(R.id.btnpost);
btnpost.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new HttpAsyncTask().execute("http://webservice1/RegistroHorario/WCFJsonRegistroHorario.svc/GetHorasDia/rcm?fecha=01/27/2014");
}
});
}
private class HttpAsyncTask extends AsyncTask<String, Void, JSONArray> {
@Override
protected JSONArray doInBackground(String... urls) {
return POST(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(JSONArray result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}
}
public static JSONArray POST(String url){
InputStream is = null;
String result ="";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("usuario","rcm"));
nameValuePairs.add(new BasicNameValuePair("contraseña","password"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("charset", "utf-8");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.w("OK", "CONECTED----------------------------------");
*************THIS IS THE POINT IM STUCK AT *****************
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
return null;
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new IputStreamReader(is,"utf-8"),10);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.w("OK", " CONVERTED---------------------------------");
}catch(Exception e){
//textView.setText("Error2: "+e.toString());
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
//JArray = new JSONParser(result);
Log.w("OK", " PERFECT -------------------------------");
return jArray;
}catch(JSONException e){
//textView.setText("Error3: "+e.toString());
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}
日志显示“已连接”后,我收到此错误: 解析数据时出错org.json.JSONException:java.lang.Integer类型的值401无法转换为JSONArray
我收到了公司提供的0条信息,他们给我的是每个所需数据的WCF URL请愿清单......
我很遗憾这个JSON问题和WCF服务请愿书,也找不到任何有用的教程,所以给出的任何信息都会很棒......
非常感谢