设置库的属性

Set attributes of libraries

我正在开发一个应用程序,我需要在其中从在线数据库中检索数据,并且我正在导入 HttpClient、HttpEntity、HttpPost 等。

现在,我遇到的问题是以下消息 "no main manifest attribute, in C:\Users(......)"

因此我打开了这些库的 manifest.mf,但我找不到任何错误,也找不到 "attribute" 字段。

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();   
?>

HttpClient 已弃用,completely removed in the latest release of Android 6.0. You should be using HttpURLConnection 改为进行网络调用。如果您出于任何原因确实需要使用 HttpClient,则必须将此行包含在清单的 android 块中以使其工作:

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

编辑:回应以下评论...

这是我编写的一些代码,用于通过网络服务访问在线数据库。它向我的服务器发出 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;