2

我正在开发一个 android 应用程序,其中使用 web 服务通过 JSON 获取数据。以下是我用来登录应用程序的 JSON 字符串。所有参数、wsfunction、syntax 都经过检查并发现是正确的。

MainActivity.java

String loginjson = "{\"userDetails\":[{\"username\":"
       +"\""
+username.getText().toString().trim()
+"\",\"password\":"
+"\""
+password.getText().toString()
+"\",\"ipaddress\":"
+"\""
+Constants.ipaddress
+"\",\"lastaccess\":\"0\"}],\"wsfunction\":\"user_authentication\"}";

Constants.sercall.servercall(loginjson);

这里还有代码,其中写入了来自服务器的请求和响应的逻辑。

服务器调用.java

import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import android.util.Log;
public class Servercalls {

        public String response_str="";
        public HttpClient httpclient = new DefaultHttpClient();
        public HttpPost httppost = new HttpPost(url); //PHP webservice url

        public void servercall(String jsonstring){
         try {
                 Log.d("Request to server",jsonstring);  
                JSONArray postjson=new JSONArray();
                    postjson.put(jsonstring);
                    httppost.setHeader("json",jsonstring);
                     httppost.getParams().setParameter("jsonpost",postjson);
                HttpResponse response;
                response = httpclient.execute(httppost);
                   if(response != null){
                InputStream is = response.getEntity().getContent();
                     BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                     String line="";
                          while ((line = reader.readLine()) != null) {
                                         response_str=line;
                                Log.e("Response from server",response_str);  
                           }         
                        }
                        } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        Log.e("cpexception", "Error in client protocol" + e.toString());
                        } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        } catch (Exception e) {
                        Log.e("log_tag", "Error in http connection "+ e.toString());
                                }
         }

}

它给出的输出为:

{"error":1,"msg":"未指定服务"}

请帮助我从服务器获得正确的响应....

4

0 回答 0