2016-06-14 28 views
0

从第一个图像在我的两个活动中,我发送请求 到php两个不同的 php文件,我得到了对android活动的有效响应。如何将数据从一个活动发送到php并从同一个php中检索数据到另一个活动?

Image 1

在第二图像,在组合上述两个PHP文件到一个 PHP文件,从活动1发送请求到PHP时,我无法 到 获取响应和活性2还。

Image 2

private void login(final String textViewNameq, String textViewNameqw,String day) { 

    class LoginAsync extends AsyncTask<String, Void, String> { 

     private Dialog loadingDialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading..."); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      String uname = params[0]; 
      String pass = params[1]; 
      String day=params[2]; 

      InputStream is = null; 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("username", uname)); 
      nameValuePairs.add(new BasicNameValuePair("password", pass)); 
      nameValuePairs.add(new BasicNameValuePair("day", day)); 
      String result = null; 

      try { 
       HttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(
         "Your php link goes here"); 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       HttpResponse response = httpClient.execute(httpPost); 

       HttpEntity entity = response.getEntity(); 

       is = entity.getContent(); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 

       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       result = sb.toString(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return result; 
     } 

和我的PHP代码放在这里,

<?php 
include("my php file path");       
// if (!empty($_POST['username']) && !empty($_POST['password'])){ 
     $Fromname = trim($_POST['username']); 
     $Toname = trim($_POST['password']); 
     //$Fromdate=$_POST['iddate']; 
     //$Fromd = date("Y-m-d", strtotime($Fromdate)); 
     $from_id=trim($_POST['idss']); 
     $to_id=trim($_POST['iddd']); 
    // } 

    $url="http://my url with API key goes here"; 


    $headers = array('Content-Type: application/json');     
      $ch = curl_init(); 
       curl_setopt($ch, CURLOPT_URL, $url); 
       curl_setopt($ch, CURLOPT_POST, true); 
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);     
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
       curl_setopt($ch, CURLOPT_ENCODING , "gzip"); 
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);       
       $page = curl_exec($ch); 
       curl_close($ch);  
       $result=json_decode($page,true);  
       if(empty($result)){     
        $page=file_get_contents($url); 
        $result=json_decode($page,true); 
       } 
+0

你添加了一个PHP标记,这段代码没有引用任何需要的PHP代码。如果您需要帮助,请编辑您使用的编程语言标签,只有PHP程序员才会看到这一点。 – KDOT

+0

显示你的php脚本 –

回答

0

有可能得到相同的PHP文件所需的响应。你需要做的就是在请求方法中发送所需的信息,并在php文件中相应地处理并生成响应。我们可以帮助你更好,如果你显示你的PHP脚本

+0

yes @ Ratul Doley我已经添加了我的php脚本。 – Kogile

+0

如果上面的代码是你的完整代码,那么它显然不会产生任何响应,即它不会为你的应用程序读取任何内容......如果$ result是你想要读取的响应,然后写入'if(!empty($ result) )echo $ result; else // some response' –

+0

如果我回显$ result,如果第一个图像中有两个php文件,但不是第二个图像中的单个php文件,我可以得到响应。 – Kogile

相关问题