我已经看到了更多这样的问题,但没有设法理解它们,或者它们不适用于我的问题,所以就这样吧。
我有一个 Activity 允许您登录,另一个您可以使用您的登录凭据发送 POST 和 GET 请求等。
主要活动:
public class MainActivity extends Activity
{
private String username;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText usernameField = (EditText) findViewById(R.id.enterUsername);
final EditText passwordField = (EditText) findViewById(R.id.enterPassword);
Button startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
username = usernameField.getText().toString();
password = passwordField.getText().toString();
Intent myIntent = new Intent(view.getContext(), HttpGetPost.class);
startActivityForResult(myIntent, 0);
}
});
}
public String getUser() { return this.username; }
public String getPassword() { return this.password; }
}
HttpGetPost:
public class HttpGetPost extends Activity
{
private MainActivity mainProxy = new MainActivity();
private Button postButton;
private Button getButton;
private Button getMeasureButton;
private Button getDevicesButton;
private String access_token;
private String refresh_token;
private String device_list;
private String expires_in;
private String getRequest;
private static final String TAG = "MyActivity";
private static final String USER_AGENT = "Mozilla/5.0";
private String clientID = some_id;
private String clientSecret = some_secret;
private String user = mainProxy.getUser();
private String pass = mainProxy.getPassword();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http);
Log.v(TAG, "mainProxy.username: "+user);
Log.v(TAG, "mainProxy.password: "+pass);
postButton = (Button) findViewById(R.id.postButton);
postButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
new sendPost().execute("");
}
});
}
private class sendPost extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
try
{
String url = some_url;
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "grant_type=password&client_id=" +clientID +"&client_secret="
+clientSecret +"&username=" +user +"&password=" +pass;
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
Log.v(TAG, "\nSending 'POST' request to URL : " + url);
Log.v(TAG, "Post parameters : " + urlParameters);
Log.v(TAG, "Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
Log.v(TAG, response.toString());
if (responseCode == 200)
{
access_token = response.substring(17, 74);
refresh_token = response.substring(93,150);
expires_in = response.substring(165, 170);
getRequest = "http://api.netatmo.net/api/getuser?access_token=" +access_token + " HTTP/1.1";
Log.v(TAG, "access token: " +access_token);
Log.v(TAG, "refresh token: " +refresh_token);
Log.v(TAG, "expires in: " +expires_in);
}
}
catch(Exception e){
e.printStackTrace();
}
return "";
}
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), "ENDED", Toast.LENGTH_LONG).show();
}
}
}
当我在第二类中打印出用户名和密码时,它们都返回 null,并且 POST 请求失败。