【发布时间】:2014-09-16 18:47:21
【问题描述】:
我试图通过发送 Https Post 请求并在“in android”登录后获取下一页的源代码来将 loginApplication 制作成网站 我看了很多视频,但没有任何效果: 请在此代码中提供任何帮助:
public class MainActivity extends Activity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.textView2);
try {
new DownloadSourceCodeTask().execute("Link");
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error in Main", Toast.LENGTH_LONG)
.show();
}
} private InputStream OpenHttpConnection(String urls) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urls);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not An Http Connction");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
String param="ssousername=" + URLEncoder.encode("usernamevalue","UTF-8")+
"password="+URLEncoder.encode("passwordvalue","UTF-8");
httpConn.setDoOutput(true);
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("POST");
httpConn.setFixedLengthStreamingMode(param.getBytes().length);
httpConn.setRequestProperty("Content-Type", "text/html; charset=WINDOWS-1256");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception e) {
Log.d("Networking", e.getLocalizedMessage());
}
return in;
}
private String DownloadSourceCode(String url){
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in=OpenHttpConnection(url);
} catch (IOException e) {
Log.d("Error in connection",e.getLocalizedMessage());
return "";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[]inputBuffer = new char[BUFFER_SIZE];
try{
while((charRead = isr.read(inputBuffer))>0){
String readString = String.copyValueOf(inputBuffer,0,charRead);
str+=readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
}catch(IOException e){
Log.d("Error in connection",e.getLocalizedMessage());
return "";
}
return str;
}
private class DownloadSourceCodeTask extends AsyncTask<String , Void , String>{
ProgressDialog dialog;
protected String doInBackground(String... urls){
return DownloadSourceCode(urls[0]);
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(MainActivity.this, "",
"Loading. Please wait...", true);
}
protected void onPostExecute(String result){
if(dialog.isShowing()){
dialog.cancel();
}
text.setText(result);
}
}
}
【问题讨论】:
-
Stack Overflow 是一个问答资源,而不是调试服务。 “请帮助使用此代码”不是问题。请解释问题是什么。在哪种情况下不起作用?你得到什么结果,它们与预期结果有何不同?包括您收到的任何错误消息,逐字逐句(不要只描述它们)。
-
我需要将用户名和密码发布到 https 网站的登录页面,好的,我在互联网上搜索了很多,但我找不到最好的解决方案,然后我找到了这段代码,但它不起作用,关于错误消息它仍然让我的应用程序运行很多工作然后诅咒,请任何帮助
标签: android post login https header