Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从.NETWebService(JSON响应)方法获取对android应用程序的响应_Android_Json_Web Services_Asmx_Webmethod - Fatal编程技术网

如何从.NETWebService(JSON响应)方法获取对android应用程序的响应

如何从.NETWebService(JSON响应)方法获取对android应用程序的响应,android,json,web-services,asmx,webmethod,Android,Json,Web Services,Asmx,Webmethod,我已经在asp.net中创建了Webservice,它给了我JSON输出,但当我从android设备调用该Webservice时,它给了我错误。 我尝试了下面的代码,但发现了错误 LoginActivity.java public class LoginActivity extends Activity { String response; ProgressDialog progressDialog; List<NameValuePair> par

我已经在asp.net中创建了Webservice,它给了我JSON输出,但当我从android设备调用该Webservice时,它给了我错误。 我尝试了下面的代码,但发现了错误

LoginActivity.java

    public class LoginActivity extends Activity {

    String response;

    ProgressDialog progressDialog;
    List<NameValuePair> parameters;

    String mUrlWebServiceLogin = "http://www.example.com/MobileWeb/WS_Login.asmx/Login";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        parameters = new ArrayList<NameValuePair>();

        parameters.add(new BasicNameValuePair("userName", "xyz"));
        parameters.add(new BasicNameValuePair("password", "abc123"));

        // Call Web Service
        new CallWebService().execute();      
    }

    public class CallWebService extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            // Call Webservice for Get Menus
            WebServiceCall webServiceCall = new WebServiceCall(); // Custom class for call webservice
            response = webServiceCall.makeServiceCall(mUrlWebServiceLogin, parameters);

            Log.d("ResponseLogin:", response);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (progressDialog.isShowing())
                progressDialog.dismiss();

            Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();
        }

        @Override
        protected void onPreExecute() {
            progressDialog = new ProgressDialog(LoginActivity.this);
            progressDialog.setMessage("Loading...");
            progressDialog.show();
            progressDialog.setCanceledOnTouchOutside(false);
            super.onPreExecute();
        }
    }
}
    public class WebServiceCall {

    static String response = null;
    public final static int POST = 2;

    public WebServiceCall() {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
    }

    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url) {
        return this.makeServiceCall(url, null);
    }

    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, List<NameValuePair> params) {
        try {

            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            HttpPost httpPost = new HttpPost(url);

            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }
}

版本信息:Microsoft.NET Framework版本:2.0.50727.5485;ASP.NET版本:2.0.50727.5491
看来你的网址错了

curl --data "userName=xyz&password=abc123" http://example.com
将您的url追加到此
?userName=xyz&password=abc123
,并在浏览器中请求。如果您得到JSON响应,您的android代码必须正常工作

编辑 要测试post请求,请使用curl

<location path="YourWebservice.asmx">
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</location>
添加到配置文件(在我得到解决方案之后)



我不熟悉.net,您确定您的服务url是正确的“[WebService(Namespace=“””。那么,如果您在真正的android设备上测试,您的服务必须可以从本地网络访问。@DanielNugent感谢您的关注。我添加了curl示例来测试post请求。我将此代码放在哪里?curl--data“userName=xyz&password=abc123“让我们。
[{"EmployeePhoto":"1003-defaultmale.png","EmployeeID":"1003","EmpName":"John Cena","EmployeementDate":"01 Oct 1994","EDateOfBirth":"21 Oct 1963","StateName":"Gujarat","BranchName":"Personnel and Admin","CategoryName":"Staff","SubCatName":"Technical","DepartmentName":"General Administration","DesignationName":"SENIOR CASHIER","PaycaderName":"E1","EProbation":"0","ECompanyEmail":"robert@gmail.com"}]
String mUrlWebServiceLogin = "http://www.example.com/MobileWeb/WS_Login.asmx/Login";
curl --data "userName=xyz&password=abc123" http://example.com
<location path="YourWebservice.asmx">
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</location>