Android向服务器发送JSON身份验证并检索数据

时间:2014-09-05 14:46:41

标签: java android json authentication

您好我想对URL进行身份验证并从中获取数据。我找到了smth,但我得到以下错误。可能是什么问题?

public class MainActivity extends Activity {

    private static final String URL = "https://mobilogr.uludag.edu.tr/BYService/LoginService.asmx/LoginUser";   

    @Override
    public void onCreate(Bundle savedInstanceState) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

       public void sendJson(View v) {

                    HttpClient client = new DefaultHttpClient();
                    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                    HttpResponse response;
                    JSONObject json = new JSONObject();

                    try {
                        HttpPost post = new HttpPost(URL);
                        json.put("kullaniciAdi", "asdsads");
                        json.put("sifre", "asdasdsa");
                        StringEntity se = new StringEntity( json.toString());  
                        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                        post.setEntity(se);
                        response = client.execute(post);

                        /*Checking response */
                        if(response!=null){
                            InputStream in = response.getEntity().getContent(); //Get the data in the entity
                        }

                    } catch(Exception e) {
                        e.printStackTrace();                        
                    }                   


        }
}

我收到以下错误:

09-05 14:37:30.632: W/System.err(1256): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
09-05 14:37:30.641: W/System.err(1256):     at org.apache.harmony.xnet.provider.jsse.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:137)
09-05 14:37:30.641: W/System.err(1256):     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
09-05 14:37:30.650: W/System.err(1256):     at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
09-05 14:37:30.661: W/System.err(1256):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
09-05 14:37:30.661: W/System.err(1256):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-05 14:37:30.671: W/System.err(1256):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-05 14:37:30.671: W/System.err(1256):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-05 14:37:30.671: W/System.err(1256):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-05 14:37:30.681: W/System.err(1256):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-05 14:37:30.691: W/System.err(1256):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-05 14:37:30.691: W/System.err(1256):     at com.example.testjson.MainActivity.sendJson(MainActivity.java:52)
09-05 14:37:30.691: W/System.err(1256):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 14:37:30.691: W/System.err(1256):     at java.lang.reflect.Method.invoke(Method.java:511)
09-05 14:37:30.691: W/System.err(1256):     at android.view.View$1.onClick(View.java:3586)
09-05 14:37:30.701: W/System.err(1256):     at android.view.View.performClick(View.java:4084)
09-05 14:37:30.701: W/System.err(1256):     at android.view.View$PerformClick.run(View.java:16966)
09-05 14:37:30.711: W/System.err(1256):     at android.os.Handler.handleCallback(Handler.java:615)
09-05 14:37:30.711: W/System.err(1256):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-05 14:37:30.720: W/System.err(1256):     at android.os.Looper.loop(Looper.java:137)
09-05 14:37:30.720: W/System.err(1256):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-05 14:37:30.731: W/System.err(1256):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 14:37:30.731: W/System.err(1256):     at java.lang.reflect.Method.invoke(Method.java:511)
09-05 14:37:30.731: W/System.err(1256):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-05 14:37:30.741: W/System.err(1256):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-05 14:37:30.741: W/System.err(1256):     at dalvik.system.NativeStart.main(Native Method)

如果有人知道该怎么做,请帮助我,谢谢大家。

1 个答案:

答案 0 :(得分:1)

请致电:

 private void loginValidate()
 {
    String username, password;
    username = _username.getText().toString();
    password = _password.getText().toString();

    if (username.equalsIgnoreCase("") && password.equalsIgnoreCase(""))
    {
        myToast("Please enter the uername and Password!!");
        return;
    }
    if (isNetworkConnected())
    {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(Central.USERNAME, username));
        params.add(new BasicNameValuePair(Central.PASSWORD, password));
        new LOGINREQUESTOR(Central.WS_LOGIN, params).execute();
    } else
        myToast("No Internet Connection!!!");

 }   


class LOGINREQUESTOR extends AsyncTask<String, Integer, String>
{

    String URL;
    List<NameValuePair> parameters;

    private ProgressDialog pDialog;

    public LOGINREQUESTOR(String url, List<NameValuePair> params)
    {
        this.URL = url;
        this.parameters = params;
    }

    @Override
    protected void onPreExecute()
    {
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Processing Request...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params)
    {
        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            HttpPost httpPost = new HttpPost(URL);

            if (parameters != null)
            {
                httpPost.setEntity(new UrlEncodedFormEntity(parameters));
            }
            httpResponse = httpClient.execute(httpPost);

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

        } catch (Exception e)
        {

        }
        return "";
    }

    @Override
    protected void onPostExecute(String result)
    {
        pDialog.dismiss();

        try
        {
            SQLDataHelper help = new SQLDataHelper(getApplicationContext());
            JSONHelper helper = new JSONHelper();
            Accounts loggedaccount = helper.getLoggedUser(result);

            String res = loggedaccount.getLogin_responce();

            if (res.equalsIgnoreCase("success"))
            {

            } else
            {

                myToast("Login Failed..");

            }

        } catch (Exception e)
        {

        }
        super.onPostExecute(result);
    }

}