1

I have a problem ConnectionError in my script python with android studio although the Internet is connected and this script is working just in native python without errors

-----------------the script testpy--------------------

import json , sys , hashlib , os , time , marshal, getpass

from importlib import reload

reload(sys)

user = ''
pass = ''


def get(data):

        r = requests.get('https://api.facebook.com/restserver.php',params=data)
        a = json.loads(r.text)
        print (a['access_token'])
        print ( 'Welcome')
        exit()
    except KeyError:
        print ( 'User or password incorrect')
    except requests.exceptions.ConnectionError:
        print ( 'Error connection ')

def access():
    API_SECRET = '62f8ce9f74b12f84c123cc23437a4a32';data = {"api_key":"882a8490361da98702bf97a021ddc14d","credentials_type":"password","email":user,"format":"JSON", "generate_machine_id":"1","generate_session_cookies":"1","locale":"en_US","method":"auth.login","password":pass,"return_ssl_resources":"0","v":"1.0"};sig = 'api_key=882a8490361da98702bf97a021ddc14dcredentials_type=passwordemail='+id+'format=JSONgenerate_machine_id=1generate_session_cookies=1locale=en_USmethod=auth.loginpassword='+pass+'return_ssl_resources=0v=1.0'+API_SECRET
    x = hashlib.new('md5')
    x.update(sig.encode('utf-8'))

    data.update({'sig':x.hexdigest()})
    get(data)

-----------------the class java--------------------

public class MainActivity extends AppCompatActivity {

    TextView txtSignIn;
    EditText inputUser,inputPass;
    PyObject pyO;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtSignIn = findViewById(R.id.txtSignIn);
        inputUser = findViewById(R.id.inputUser);
        inputPass = findViewById(R.id.inputPass);



        if (!Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }

        Python python = Python.getInstance();

        pyO = python.getModule("testpy");

        txtSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pyO.put("user", inputUser.getText().toString());
                pyO.put("pass", inputPass.getText().toString());

                pyO.callAttr("access");

            }
        });

    }
}
The Dark
  • 71
  • 2
  • 7
  • Did you include the INTERNET permission as described [here](https://stackoverflow.com/questions/2378607/)? If that doesn't help, please add the ConnectionError message and stack trace to your question. You can make it appear in the logcat by removing the ConnectionError catch block. – mhsmith Nov 01 '19 at 15:07
  • I already included the INTERNET permission and when I removed ConnectionError catch block this is the error : (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 1] Operation not permitted',)) – The Dark Nov 01 '19 at 16:02
  • Please edit your question to add the full stack trace. The edit link is above these comments, where it says "share edit close flag". – mhsmith Nov 02 '19 at 14:28
  • Also, there are several syntax errors in your Python code: (1) missing `try` in the `get` function; (2) using the keyword `pass` as a variable name; (3) using `id` where I think you meant `user`. You must have fixed these in order to get as far as this exception, so please edit the question to show the current version of your code. – mhsmith Nov 02 '19 at 14:35

0 Answers0