将数据从Android应用程序发布到PHP web服务

将数据从Android应用程序发布到PHP web服务,android,string,http-post,bundle,Android,String,Http Post,Bundle,我遇到了一个非常奇怪的错误。只有在我定义nameValuePairs.add(新的BasicNameValuePairs(“国家”、“美国”))时,下面的方法才能将数据发布到我的PHP web服务并检索JSON编码的数据但是,如果我使用字符串变量country而不是“USA”,我的web服务将得到null。我已经检查了字符串country的值,它不是null。下面是我的代码 此处定义了我的字符串国家: public class Countries extends SupportMapFragme

我遇到了一个非常奇怪的错误。只有在我定义nameValuePairs.add(新的BasicNameValuePairs(“国家”、“美国”))时,下面的方法才能将数据发布到我的PHP web服务并检索JSON编码的数据但是,如果我使用字符串变量country而不是“USA”,我的web服务将得到null。我已经检查了字符串country的值,它不是null。下面是我的代码

此处定义了我的字符串国家:

public class Countries extends SupportMapFragment implements
    LocationListener, LocationSource
{
private GoogleMap map;
private OnLocationChangedListener mListener;
private LocationManager locationManager;
double mLatitude = 0;
double mLongitude = 0;
String country = "";
.......
    @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
           .......
 }
字符串country通过Bundle对象从以前的活动中获取值。该值不为空

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{

    View root = super.onCreateView(inflater, container, savedInstanceState);
    Bundle bundle = new Bundle();
    bundle = getArguments();
    if(bundle == null)
        Toast.makeText(getActivity(), "Country is NULL",
                Toast.LENGTH_LONG).show();
    else
    {

        country = getArguments().getString("countryName");


    }
    map = getMap();
    return root;
}
这是我发布数据的方法。

    private String postCountryType()
{
    String responseStr = "";

    try
    {
        // url where the data will be posted
        String postReceiverUrl =      "http://.../country.php";

        // HttpClient
        HttpClient httpClient = new DefaultHttpClient();

        // post header
        HttpPost httpPost = new HttpPost(postReceiverUrl);

        // add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

        nameValuePairs.add(new BasicNameValuePair("country", country));

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // execute HTTP post request
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();

        if(resEntity != null)
        {

            responseStr = EntityUtils.toString(resEntity).trim();

            // you can add an if statement here and do other actions based
            // on the response
        }

    }
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return responseStr;
}
private String postCountryType()
{
字符串responsest=“”;
尝试
{
//将发布数据的url
字符串postReceiverUrl=”http://.../country.php";
//HttpClient
HttpClient HttpClient=新的DefaultHttpClient();
//柱头
HttpPost HttpPost=新的HttpPost(postReceiverUrl);
//添加您的数据
List nameValuePairs=新的ArrayList(2);
nameValuePairs.添加(新的BasicNameValuePairs(“国家”,国家));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP post请求
HttpResponse response=httpClient.execute(httpPost);
HttpEntity当前性=response.getEntity();
if(最近性!=null)
{
responsest=EntityUtils.toString(resEntity.trim();
//您可以在此处添加if语句,并根据需要执行其他操作
//关于回应
}
}
捕获(客户端协议例外e)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
返回响应;
}
当我使用字符串变量country而不是将数据“USA”直接传递到新的BasicNameValuePair时,ResponseStr返回null

我已尝试将新的BasicNameValuePair(“country”,country)修改为新的BasicNameValuePair(“country,country.toString()),以及新的BasicNameValuePair(“country,country+”) 但不幸的是,这两种伎俩都没有奏效

其他信息 但是,如果我定义字符串country=“USA”,在onCreateView中,值“Japan”实际上是通过getArguments分配给country的。BasicNameValuePair(“国家”,country)将忽略值“Japan”,但使用原始值“USA”

有人知道为什么会发生这种奇怪的事情吗


提前感谢。

国家
可能不为空,但您是否检查过它是否为空字符串
“”
?这就是在
国家/地区
类中初始化的内容。我将执行
Log.d(“调试”,国家/地区)
,并检查LogCat以查看在您创建
NameValuePair

问题解决时国家/地区中的实际情况。我将onCreate()替换为onActivityCreated()。根据片段的生命周期,字符串country的值将在片段onActivityCreated()之前检索。

Hi Samuel,我忘了提到的另一件事是,当我在postCountryType()方法中放入Log.d或Log.e或Log.I时,不会记录任何内容。然而,我确实在调试模式下运行了我的应用程序,从那里我发现country的值是“USA”,它不是空的。