2015-10-19 16 views
0

我開發一個應用程序中,我需要從一個在線數據庫中檢索數據,我進口的HttpClient,HttpEntity,HttpPost的屬性集等。現在

,我有問題的是下面的消息「不主要清單屬性,在C:\ Users(......)「

因此我打開了這些庫的manifest.mf,但我找不到任何錯誤和」屬性「字段。

HttpClient的

Manifest-Version: 1.0 
Implementation-Title: HttpComponents Apache HttpClient Cache 
Implementation-Version: 4.5.1 
Built-By: oleg 
Specification-Vendor: The Apache Software Foundation 
Created-By: Apache Maven 3.0.5 
url: http://hc.apache.org/httpcomponents-client 
X-Compile-Source-JDK: 1.6 
Implementation-Vendor: The Apache Software Foundation 
Implementation-Vendor-Id: org.apache 
Build-Jdk: 1.7.0_75 
Specification-Title: HttpComponents Apache HttpClient Cache 
Specification-Version: 4.5.1 
Implementation-Build: tags/4.5.1-RC1/[email protected]; 2015-0 
9-11 14:53:18+0200 
X-Compile-Target-JDK: 1.6 
Archiver-Version: Plexus Archiver 

我找不到上StackOverfFlow其他地方也不在互聯網上的任何幫助的

例子:我怎樣才能設置屬性?我還應該在Gradle文件上工作嗎?而且,我是否必須以相同的方式實現所有庫的清單?

在此先感謝

編輯:

我只是替換HttpURLConnection的,但我有問題,你碰巧知道爲什麼嗎?

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 

TextView resultView; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    StrictMode.enableDefaults(); //STRICT MODE ENABLED 
    resultView = (TextView) findViewById(R.id.result); 
    String line=null; 
    String [] stream_name; 
    HttpURLConnection urlConnection = null; 
    String value; 
    getData(); 
} 

public void getData(){ 
    String result = ""; 
    InputStream isr = null; 
    try{ 
     URL url = new URL ("xxxxxxxxxxxxxxx"); 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.connect(); 
     isr = urlConnection.getInputStream(); 

    } 
    catch(Exception e){ 
     Log.e("log_tag", "Error in http connection " + e.toString()); 
     resultView.setText("Couldn't connect to database"); 
    } 
    //convert response to string 
    try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     isr.close(); 

     result=sb.toString(); 
    } 
    catch(Exception e){ 
     Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

    //parse json data 
    try { 
     String s = ""; 
     JSONArray jArray = new JSONArray(result); 

     for(int i=0; i<jArray.length();i++){ 
      JSONObject json = jArray.getJSONObject(i); 
      s = s + 
        "Price : "+json.getString("Price")+"\n"+ 
        "Weight : "+json.getInt("Weight")+"\n"+ 
        "Price/Weight : "+json.getString("P/W")+"\n\n"; 
     } 

     resultView.setText(s); 

    } catch (Exception e) { 
     // TODO: handle exception 
     Log.e("log_tag", "Error Parsing Data "+e.toString()); 
    } 
} 

}

我的PHP文件是

<?php 

    // PHP variable to store the host address 
$db_host = "xxxxx"; 
// PHP variable to store the username 
$db_uid = "xxxxxx"; 
// PHP variable to store the password 
$db_pass = "xxxxxxxx"; 
// PHP variable to store the Database name 
$db_name = "xxxxxxx"; 
     // PHP variable to store the result of 

the PHP function 'mysql_connect()' which 

establishes the PHP & MySQL connection 
$db_con = mysql_connect($db_host,$db_uid, 

$db_pass) or die('could not connect'); 
mysql_select_db($db_name); 
$sql = "SELECT * FROM TABLE 1 WHERE ID = 'Bread AH'"; 
$result = mysql_query($sql); 
while($row=mysql_fetch_assoc($result)) 
    $output[]=$row; 
print(json_encode($output)); 
mysql_close(); 
?> 

回答

0

HttpClient的被棄用,並於completely removed in the latest release of Android 6.0。您應該使用HttpURLConnection來代替撥打網絡電話。如果你確實需要使用HttpClient無論出於何種原因,你必須包括在清單的android塊這一行,使其工作:

android { 
    useLibrary 'org.apache.http.legacy' 
} 

編輯:在回答下面的評論.. 。

這是我寫的一些代碼,通過Web服務訪問在線數據庫。它向我的服務器發送一個POST請求,發送一些JSON數據,並接收JSON作爲響應,稍後在代碼中解析。

//The JSON we will get back as a response from the server 
    JSONArray jsonResponse = null; 

    //Http connections and data streams 
    URL url; 
    HttpURLConnection httpURLConnection = null; 
    OutputStreamWriter outputStreamWriter = null; 

    try { 

     //open connection to the server 
      url = new URL("your_url_to_web_service"); 
      httpURLConnection = (HttpURLConnection) url.openConnection(); 

      //set request properties 
      httpURLConnection.setDoOutput(true); //defaults request method to POST 
      httpURLConnection.setDoInput(true); //allow input to this HttpURLConnection 
      httpURLConnection.setRequestProperty("Content-Type", "application/json"); //header params 
      httpURLConnection.setRequestProperty("Accept", "application/json"); //header params 
      httpURLConnection.setFixedLengthStreamingMode(jsonToSend.toString().getBytes().length); //header param "content-length" 

      //open output stream and POST our JSON data to server 
      outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream()); 
      outputStreamWriter.write(jsonToSend.toString()); 
      outputStreamWriter.flush(); //flush the stream when we're finished writing to make sure all bytes get to their destination 

      //prepare input buffer and get the http response from server 
      StringBuilder stringBuilder = new StringBuilder(); 
      int responseCode = httpURLConnection.getResponseCode(); 

      //Check to make sure we got a valid status response from the server, 
      //then get the server JSON response if we did. 
      if(responseCode == HttpURLConnection.HTTP_OK) { 

       //read in each line of the response to the input buffer 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8")); 
       String line; 
       while ((line = bufferedReader.readLine()) != null) { 
        stringBuilder.append(line).append("\n"); 
       } 

       bufferedReader.close(); //close out the input stream 

      try { 
       //Copy the JSON response to a local JSONArray 
       jsonResponse = new JSONArray(stringBuilder.toString()); 
      } catch (JSONException je) { 
       je.printStackTrace(); 
      } 

    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } finally { 
     if(httpURLConnection != null) { 
      httpURLConnection.disconnect(); //close out our http connection 
     } 

     if(outputStreamWriter != null) { 
      try { 
       outputStreamWriter.close(); //close our output stream 
      } catch (IOException ioe) { 
       ioe.printStackTrace(); 
      } 
     } 
    } 

    //Return the JSON response from the server. 
    return jsonResponse; 
+0

嗨@drschultz,感謝您的反饋。我的目的是從在線MySQl獲取數據並將其顯示在活動中。 HttpURLConnection會適合它嗎? –

+0

是的! 'HttpURLConnection'非常適合從在線數據庫獲取數據,並且是Google在官方文檔中建議的推薦方法。我將在我的一個方法的上面添加一些示例代碼,這些方法用於查詢Web服務以使用'HttpURLConnection'從在線數據庫獲取數據。 – NoChinDeluxe

+0

嗨DrSchultz,我無法在Android Studio中找到它。我如何設法得到它? –