user5899421 February 2016

please help me i can not solve the code with HttpUrlConnection and all new for api 23

i am trying to make a login check from sever i have found this code from internet but now HttpClient and some other are deprecated for api 23 and higher. but i am unable to modify this with HttpUrlConnection,libray and all updates. so please need help badly.

main Activity:

package com.example.login;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginLayout extends Activity {
    EditText un,pw;
    TextView error;
    Button ok;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        un=(EditText)findViewById(R.id.et_un);
        pw=(EditText)findViewById(R.id.et_pw);
        ok=(Button)findViewById(R.id.btn_login);
        error=(TextView)findViewById(R.id.tv_error);

        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
                postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));

                String response = null;
                try {
                    response = CustomHttpClient.executeHttpPost("<target page url>", postParameters);
                    String res=response.toString();
                    res= res.replaceAll("\\s+","");
                    if(res.equals("1"))
                        error.setText("Correct Username or Password");
                         

Answers


JustinMorris February 2016

While it has been deprecated you can still use it by adding this to your Gradle file:

android {
  compileSdkVersion 23
  buildToolsVersion "23.0.2"
  useLibrary 'org.apache.http.legacy'

  ...
}

Post Status

Asked in February 2016
Viewed 1,263 times
Voted 14
Answered 1 times

Search




Leave an answer