设置库的属性

时间:2015-10-19 22:12:00

标签: android apache https android-manifest libraries

我正在开发一个应用程序,我需要从在线数据库中检索数据,我正在导入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/httpclient-cache@r1702448; 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();   
?>

1 个答案:

答案 0 :(得分: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;