简体   繁体   中英

Android Send JSON Authentication to server and retrieve data

Hello i want to authenticate on the URL and get data from there. I found smth but i get the following error. What can be the problem?

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();                        
                    }                   


        }
}

I get the following error:

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)

If someone knows what to do please help me on it, thanks everyone.

Call this By:

 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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM