在片段选项卡下将数据发送到服务器

时间:2014-09-09 09:22:32

标签: android android-fragments android-json

我在编程方面真的很新。我正在使用标签创建应用程序,每个标签都是一个片段,在此标签的一个标签中,我想将一些数据发送到服务器。这里是代码,看起来像所有的get()函数在片段下不起作用。 (我已经解决了这个问题)现在程序无法运行。

这是我的代码

public class TabActivityMain extends Fragment implements OnClickListener {

TextView tvIsConnected;
EditText etName,etCountry,etTwitter;
Button btnPost;
Person person;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View V = inflater.inflate(R.layout.activity_tab_activity_main, container, false);


        tvIsConnected = (TextView) V.findViewById(R.id.tvIsConnected);
        etName = (EditText) V.findViewById(R.id.etName);
        etCountry = (EditText) V.findViewById(R.id.etCountry);
        etTwitter = (EditText) V.findViewById(R.id.etTwitter);
        btnPost = (Button) V.findViewById(R.id.btnPost);

        // check if you are connected or not
        if(isConnected()){
            tvIsConnected.setBackgroundColor(0xFF00CC00);
            tvIsConnected.setText("You are conncted");
        }
        else{
            tvIsConnected.setText("You are NOT conncted");
        }

        // add click listener to Button "POST"
        btnPost.setOnClickListener(this);
        return V;
    }
public static String POST(String url, Person person){
    InputStream inputStream = null;
    String result = "";
    try {

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = "";

        // 3. build jsonObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("name", person.getName());
        jsonObject.accumulate("country", person.getCountry());
        jsonObject.accumulate("twitter", person.getTwitter());

        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();

        // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
        // ObjectMapper mapper = new ObjectMapper();
        // json = mapper.writeValueAsString(person); 

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the content   
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // 10. convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    // 11. return result
    return result;
}

public boolean isConnected(){
    ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Activity.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) 
            return true;
        else
            return false;    
}
@Override
public void onClick(View view) {

    switch(view.getId()){
        case R.id.btnPost:
            if(!validate())
                Toast.makeText(getActivity().getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
            // call AsynTask to perform network operation on separate thread
            new HttpAsyncTask().execute("http://hmkcode.appspot.com/jsonservlet");
        break;
    }

}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        person = new Person();
        person.setName(etName.getText().toString());
        person.setCountry(etCountry.getText().toString());
        person.setTwitter(etTwitter.getText().toString());

        return POST(urls[0],person);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getActivity().getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
   }
}

private boolean validate(){
    if(etName.getText().toString().trim().equals(""))
        return false;
    else if(etCountry.getText().toString().trim().equals(""))
        return false;
    else if(etTwitter.getText().toString().trim().equals(""))
        return false;
    else
        return true;    
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

} 

}

并且log cat信息是

09-09 09:30:59.907: E/AndroidRuntime(1311): FATAL EXCEPTION: main
09-09 09:30:59.907: E/AndroidRuntime(1311): Process: com.zanqi.mainpage1, PID: 1311
09-09 09:30:59.907: E/AndroidRuntime(1311): java.lang.SecurityException: ConnectivityService: Neither user 10063 nor current process has android.permission.ACCESS_NETWORK_STATE.
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.os.Parcel.readException(Parcel.java:1465)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.os.Parcel.readException(Parcel.java:1419)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.net.IConnectivityManager$Stub$Proxy.getActiveNetworkInfo(IConnectivityManager.java:813)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:563)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at com.zanqi.mainpage1.TabActivityMain.isConnected(TabActivityMain.java:128)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at com.zanqi.mainpage1.TabActivityMain.onCreateView(TabActivityMain.java:58)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:283)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.View.dispatchAttachedToWindow(View.java:12585)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2458)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1217)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at   android.view.Choreographer.doCallbacks(Choreographer.java:574)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.Choreographer.doFrame(Choreographer.java:544)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.os.Handler.handleCallback(Handler.java:733)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.os.Looper.loop(Looper.java:136)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at java.lang.reflect.Method.invoke(Method.java:515)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-09 09:30:59.907: E/AndroidRuntime(1311):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

return V; 

必须位于函数的末尾,因为返回总是会退出函数并停止编译其余代码。
至于你的Logcat:添加

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

位于AndroidManifest.xml文件中的应用程序标记上方。

答案 1 :(得分:1)

当您不再参加活动时,您需要为大多数这些功能设置上下文。

例如,findViewByID需要知道要查看的视图。

因此,在您的示例中,您将使用

V.findViewByID(..)

虽然通常你会使用getView()。findViewByID

而在另一种情况下,你一直在寻找

getActivity().getSystemService

以下是获取上下文的不同方法的一个很好的解释: https://stackoverflow.com/a/9606712/3009199

通常onCreateView看起来像这样:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_tab_activity_main, container, false);
}

然后将onCreateView中的其余代码移动到onActivityCreated

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //...more code goes here and you can easily just do getView().findViewByID
}

请注意你的回归V;被称为早期回归。一旦方法命中一个return语句,它就会执行它并且该方法中的其余代码“死”。

此外,您不应该访问AsyncTask中的UI线程。 AsyncTask正在打开一个新线程,并不一定能访问UI。因此,您应该将包含所有人员详细信息的对象传递到AsyncTask中。

最后一件事,这是偏好或风格,我相信人们会不同意。我会说作为新手避免if和while语句没有大括号是一个好习惯:

为:

if(whatever == 1)
    //blah blah

相反,你应该隐含你的括号:

好:

if(whatever == 1){
    //blah blah
}
祝你好运!