java- android-自定义HttpClient无法解析

时间:2014-12-14 03:31:30

标签: android eclipse

我做了一个简单的Android应用程序。所有代码似乎都适合我,因为我遵循了一些网站的教程,但这1行错误不会改变

它说" customhttpclient无法解析",但我已导入 " import org.apache.http.client.HttpClient;"

这是代码:

{        
      package com.android.string;

import java.io.BufferedReader;

import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {
EditText kode_parkir, license_plate;
Button simpan;

@Override
public void onCreate(Bundle savedInstanceState)
{   super.onCreate(savedInstanceState);
try
    {
    setContentView(R.layout.activity_main);
    kode_parkir = (EditText)findViewById(R.id.Parkiran);
    license_plate = (EditText)findViewById(R.id.Plate);
    simpan = (Button)findViewById(R.id.btnKirim);
    simpan.setOnClickListener (new OnClickListener() 
            {      
            @Override
            public void onClick(View v) {
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("kode_parkir", kode_parkir.getText().toString()));
                postParameters.add(new BasicNameValuePair("license_plate", license_plate.getText().toString()));
                String response = null;
                try     {
                    response = CustomHttpClient.executeHttpPost("http://192.168.73.1/android/android.php", postParameters);
                    String res = response.toString();
                    res = res.trim();
                    res = res.replaceAll("\\s", "");
                    if(res.equals("1"))
                        Toast.makeText(MainActivity.this, "Data Tersimpan", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this,  "Data Tersimpan ke Server", Toast.LENGTH_LONG).show();

                        }
                catch (Exception e) {
                    Toast.makeText(MainActivity.this, "Error : "+ e.toString(), Toast.LENGTH_LONG).show();
                                    }
                                        }
            });
    }
    catch (Exception e) {
        Toast.makeText(MainActivity.this, "Error : "+e.getMessage(), Toast.LENGTH_LONG).show();

                        }

    }

}

}       

我是eclipse的新手,所以任何帮助都会得到满足

1 个答案:

答案 0 :(得分:2)

您需要为CustomHttpClient类添加导入。

这行代码

response = CustomHttpClient.executeHttpPost("http://192.168.73.1/android/android.php", postParameters);

引用CustomHttpClient类,但该类没有import语句。在您的帖子中,您声明已包含&#34; org.apache.http.client.HttpClient&#34;的导入,但这是一个不同的类。

我不清楚CustomHttpClient的来源。它看起来不像Apache类。在任何情况下,为此类添加import语句应该可以解决问题。