2014-07-25 44 views
0

我試圖通過發送一個Https發佈請求並在登錄後爲下一個頁面創建源代碼「in Android」 i'v觀看很多視頻但嘗試通過登錄應用程序進入網站什麼工作: 任何幫助,請在此代碼:https發佈請求在Android中不起作用

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); 
     } 
    } 

} 
+0

堆棧溢出是Q&A資源,而不是調試服務。 「請幫助這個代碼」不是一個問題。請解釋什麼是問題。它以哪種方式不起作用?你得到的結果是什麼,它們與期望的結果有什麼不同?包括您收到的任何錯誤消息,逐字(不要只描述它們)。 –

+0

我需要在https網站上登錄頁面的用戶名和密碼好的我在互聯網上搜索很多,但icannt找到最好的解決方案,然後我發現這個代碼,但它不工作,,關於它仍然給我的應用程序的錯誤消息運行到很多工作,然後詛咒,任何幫助,請 – SaeedDroid

回答

0

嘗試增加一個「&」的密碼參數前,加入2線連接後(),並在內容類型更改編碼爲UTF-8。請參閱下面的新代碼:

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(); 
     httpConn.getOutputStream().write(param.getBytes()); 
     httpConn.getOutputStream().flush(); 
     response = httpConn.getResponseCode(); 
     if (response == HttpURLConnection.HTTP_OK) { 
      in = httpConn.getInputStream(); 
     } 
    } catch (Exception e) { 
     Log.d("Networking", e.getLocalizedMessage()); 
    } 

希望它有幫助。

+0

仍然粉碎,對不起,但你知道正確的方式,我得到的鏈接,我發送的數據,並使登錄因爲可能是錯誤的鏈接 – SaeedDroid