尽管 Internet 已连接并且此脚本仅在本机 python 中运行而没有错误,但我在带有 android studio 的脚本 python 中遇到了 ConnectionError 问题
-----------------脚本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)
-----------------类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");
}
});
}
}