当前位置: 首页 > 知识库问答 >
问题:

http中的Android web服务错误

单于翰飞
2023-03-14
class httpclass {
    String result;

    public String server_conn(String user_url) 
    {
        // String user_url="";

        String user_url3=user_url.replaceAll(" ","%20");
        String user_url2=user_url3.replaceAll("\n","%0D");


        HttpClient client = new DefaultHttpClient();
        HttpGet siteRequest = new HttpGet(user_url2);
        StringBuilder sb = new StringBuilder();

        HttpResponse httpResponse;

        try {
            httpResponse = client.execute(siteRequest);
            HttpEntity entity = httpResponse.getEntity();
            InputStream in = entity.getContent();

            String line = null;
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in));
            while ((line = reader.readLine()) != null)
            {

                sb.append(line);

            }

            result = sb.toString();

        } catch (ClientProtocolException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

}
public class LoginForm extends FragmentActivity {
    /** Called when the activity is first created. */

    TextView txt1, txt2, err,forget;
    EditText name;
    EditText pass;
    Button click,vend;
    CheckBox savepass;
    Button newuser;
    Button signin;
    @SuppressWarnings("unused")
    private Cursor signin1;


    SharedPreferences sharedPreferences=null;

    public static String str1, str2;
    public static String result;
    public  static String username;
    ProgressDialog myProgressDialog = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       BugSenseHandler.initAndStartSession(this, "68640bea");
        setContentView(R.layout.login);


        vend=(Button)findViewById(R.id.vend);
        name = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);
        savepass=(CheckBox)findViewById(R.id.savepass);
        Button  cancel = (Button) findViewById(R.id.cancel);
        //Button  back = (Button) findViewById(R.id.back);
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {   

                Intent second = new Intent( LoginForm.this,canceluser.class);   

                startActivity(second);
                finish();

            }

        });

        sharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);

        String name1=sharedPreferences.getString("p_name", "");
        name.setText(name1.toString());

        String pass1=sharedPreferences.getString("p_pass", "");
        pass.setText(pass1.toString());


       //s forget=(TextView)findViewById(R.id.forget);
        signin = (Button) findViewById(R.id.signin);
        click = (Button) findViewById(R.id.click);
        newuser = (Button) findViewById(R.id.signup);
        vend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent viewIntent =
                          new Intent("android.intent.action.VIEW",
                            Uri.parse("http://www.iwedplanner.com/vendor/vendorhome.aspx"));
                          startActivity(viewIntent);
            }});

        click.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(LoginForm.this, forgetpwd.class);
                startActivity(intent1);
                finish();
            }});

        signin.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) {


                if(name.getText().toString().equals(""))
                {
                alertbox("Message!","Enter Your Username");
                name.requestFocus();
                }
            else if(pass.getText().toString().equals(""))
                {
                alertbox("Message!","Enter Your Password");
                pass.requestFocus();
                }
                else
                {
                str1 = name.getText().toString();
                str2 = pass.getText().toString();

                boolean value = false;
                // validuser();
                ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);


                NetworkInfo info = cm.getActiveNetworkInfo();
                if (info != null && info.isAvailable()) {
                               value = true;


                    openconn(str1, str2);


                }
                else
                {
                    alertbox("Message!", "No Internet Connection!");
                }
            }
            }

        });

        newuser.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                newuser();

            }
        });

    }

    public void openconn(String strr1, String strr2) 
    {

        if (!strr1.equals("") && !strr2.equals(""))

        {
            err = (TextView) findViewById(R.id.err);

        //  String user_url = "http://iwedplanner.com/mobile/MLogin.aspx?uname="+ strr1 + "&pwd=" + strr2;

            String user_url="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/MLogin.aspx?uname="+ strr1 + "&pwd=" + strr2;



            httpclass obj = new httpclass();
            result = obj.server_conn(user_url);

          //  alertbox("",""+result);

            if (result != null) 
            {
                StringTokenizer st = new StringTokenizer(result, "|");
                result = st.nextToken();

                if (result.equals("InvalidUser ")) 
                {


                    Dialog locationError = new AlertDialog.Builder(
                            LoginForm.this).setIcon(0).setTitle("Message!")
                            .setPositiveButton(R.string.yes, null).setMessage(
                                    "Sorry, Invalid Username or Password ")
                            .create();
                    locationError.show();
                    name.requestFocus();
                } 
                else if(result.equals(strr1)) 
                {
                    // Toast.makeText(getBaseContext(),"Valid User",Toast.LENGTH_SHORT).show();

                    if(savepass.isChecked())
                    {
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putString("p_name",name.getText().toString());                        
                        //editor.putString("p_pass",pass.getText().toString());
                        editor.commit();
                    }
                    else
                    {
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putString("p_name", "");
                        editor.putString("p_pass","");
                        editor.commit();
                    }
                    validuser();
                }

                else
                {
                    alertbox("Message!","Error in network connection");
                }
            }

        }
    }

    public void validuser() 
    {

    username=str1;

        name.setText("");
        pass.setText("");           
        Intent userintent = new Intent(this, welcomeuser1.class);
        //userintent.putExtra("name5",str1);    

      //Intent userintent=new Intent(this,WeddingInfo.class);
        startActivity(userintent);
        finish();   



    }

    public void newuser() {

        Intent userintent1 = new Intent(this, newuserform.class);
        startActivity(userintent1);
        finish();
    }
    protected void alertbox(String title, String mymessage) {
        new AlertDialog.Builder(this)
                .setMessage(mymessage)
                .setTitle(title)
                .setCancelable(true)
                .setNeutralButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                            }
                        }).show();
    }
    @Override
      public void onStart() {
        super.onStart();
       // The rest of your onStart() code.
        // //  EasyTracker.getInstance(this).activityStart(this);  // Add this method.
      }

      @Override
      public void onStop() {
        super.onStop();
        // The rest of your onStop() code.
        // EasyTracker.getInstance(this).activityStop(this);  // Add this method.
      }
}
Path in archive: META-INF/LICENSE.txt

Origin 1: C:\Users\sentientit\Documents\Wed Studio\app\libs\twitter4j.jar

1起源2:C:\users\sentientit.gradle\caches\modules-2\files-2.1\joda-time\joda-time\joda-time\2.4\89e9725439adffbbd41c5f5c215c136082b34a7f\joda-time-2.4.jar

您可以忽略构建中的这些文件。Gradle:

android {
  packagingOptions {
    exclude 'META-INF/LICENSE.txt'
  }
}

错误:任务“:app:PackageDebug”执行失败。

在APK META-INF/license.txt文件1:C:\users\sentientit\documents\wed studio\app\libs\twitter4j.jar中复制的重复文件

File 2: C:\Users\sentientit\.gradle\cache``s\modules-2\files-2.1\joda-time\joda-time\2.4\89e9725439adffbbd41c5f5c215c136082b34a7f\joda-time-2.4.jar

共有1个答案

梁丘诚
2023-03-14

您可以这样做:

Web服务的AsyncTask:

private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
    ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("Loading...");
        pdLoading.show();
    }
    @Override
    protected Void doInBackground(Void... params) {

        String serverGETResponse = getJsonDataStringFormat("Your_Url", "GET", "", "LOGIN_ACTIVITY");
        String serverPOSTResponse = getJsonDataStringFormat("Your_Url", "POST", "YOUR_JSON_STRING", "LOGIN_ACTIVITY");    
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //this method will be running on UI thread

        pdLoading.dismiss();
       }

    }

现在从后台线程中获取服务器的响应:

public static String getJsonDataStringFormat(String url, String method,String jObjStr, String tag) {
        InputStream is = null;
        String Root_Response = null;
        HttpResponse httpResponse;
        HttpParams httpParameters = new BasicHttpParams();
        DefaultHttpClient httpClient;
        HttpConnectionParams.setConnectionTimeout(httpParameters,connectionTimeOut);
        HttpConnectionParams.setSoTimeout(httpParameters, socketTimeOut);
        try {
            httpClient = new DefaultHttpClient(httpParameters);
            url = url.replace(" ", "%20");
            if (method == "POST") {
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new StringEntity(jObjStr));
                httpResponse = httpClient.execute(httpPost);
                is = httpResponse.getEntity().getContent();
            } else if (method == "GET") {
                HttpGet httpGet = new HttpGet(new URI(url));
                httpResponse = httpClient.execute(httpGet);
                is = httpResponse.getEntity().getContent();
            }

            Root_Response = convertStreamToString(is);
            Log.i(tag, Root_Response);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (URISyntaxException e) {
            e.printStackTrace();
        }

        return Root_Response;
    }
public static String convertStreamToString(InputStream inputStream)
            throws IOException {
        if (inputStream != null) {
            StringBuilder sb = new StringBuilder();
            String line;
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream, "UTF-8"));
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }
            } finally {
                inputStream.close();
            }
            return sb.toString();
        } else {
            return "";
        }
    }
 类似资料:
  • 问题内容: 我想为多个客户端在php中为服务器创建http套接字连接。我怎样才能做到这一点 ?我需要一些资源。 首先,我试图在java中创建服务器。我在java中创建服务器。并尝试从android应用程序访问。但是服务器找不到任何客户端。但是当我在java中创建客户端时,它正在工作。我该如何解决这个问题? 问题答案: 看一下这篇文章: Zend 用PHP编写套接字服务器 还可以尝试使用Google

  • 本章主要介绍: HTTP 协议 Requests 库的使用

  • 主要内容:1.RPC 架构,2.同步调用与异步调用,3.流行的 RPC 框架,4.HTTP 服务,5.总结1.RPC 架构 2.同步异步调用 3.流行的 RPC 框架 1.RPC 架构 先说说 RPC 服务的基本架构吧。我们可以很清楚地看到,一个完整的 RPC 架构里面包含了四个核心的组件。 Client Server Client Stub Server Stub(这个Stub大家可以理解为存根) 客户端(Client),服务的调用方。 服务端(Server),真正的服务提供者。 客户端存根,

  • 主要内容:1.RPC 架构,2.同步调用与异步调用,3.流行的 RPC 框架,4.HTTP 服务,5.总结1.RPC 架构 先说说 RPC 服务的基本架构吧。我们可以很清楚地看到,一个完整的 RPC 架构里面包含了四个核心的组件。 Client Server Client Stub Server Stub(这个Stub大家可以理解为存根) 客户端(Client),服务的调用方。 服务端(Server),真正的服务提供者。 客户端存根,存放服务端的地址消息,再将客户端的请求参数打包成网络消息,然后

  • ?> 完全协程化的Http服务器实现,Co\Http\Server继承自Co\Server,在此不再赘述,只说差异。 与 Http\Server 的不同之处: 可以在运行时动态地创建、销毁 对连接的处理是在单独的子协程中完成,客户端连接的Connect、Request、Response、Close是完全串行的 !> 需要v4.4.0或更高版本 !> 若编译时开启HTTP2,则默认会启用HTTP2协