我開發一個應用程序中,我需要從一個在線數據庫中檢索數據,我進口的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();
?>
嗨@drschultz,感謝您的反饋。我的目的是從在線MySQl獲取數據並將其顯示在活動中。 HttpURLConnection會適合它嗎? –
是的! 'HttpURLConnection'非常適合從在線數據庫獲取數據,並且是Google在官方文檔中建議的推薦方法。我將在我的一個方法的上面添加一些示例代碼,這些方法用於查詢Web服務以使用'HttpURLConnection'從在線數據庫獲取數據。 – NoChinDeluxe
嗨DrSchultz,我無法在Android Studio中找到它。我如何設法得到它? –