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

使用okhttp发送http post的Android应用程序崩溃

龙枫
2023-03-14
btnLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
            //Intent mainMenu = new Intent(LoginActivity.this, MainMenu.class);
            //LoginActivity.this.startActivity(mainMenu);

            LoginImpl li = new LoginImpl();
            int userID = li.login(txtUsername.getText().toString(), txtPassword.getText().toString());

            new AlertDialog.Builder(LoginActivity.this)
                    .setTitle("TEST")
                    .setMessage("USER ID = " + userID)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .show();
        }

    }
});
package diplomski.thop.thopmobileclient.controller.requests;
import java.io.IOException;
import com.google.gson.Gson;
import diplomski.thop.thopmobileclient.controller.implementation.Urls;
import diplomski.thop.thopmobileclient.model.User;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginRequest {

    OkHttpClient client;

    public LoginRequest() {
        client = new OkHttpClient();
    }

    public int sendLoginRequest(String username, String password) {
        User user = new User(username, password);
        String requestJson = new Gson().toJson(user);
        RequestBody body = RequestBody.create(Urls.JSON, requestJson);
        Request request = new Request.Builder().url(Urls.LOGIN_URL).post(body).build();
        try (Response response = client.newCall(request).execute()) {
            String returnResponse = response.body().string();
            if (returnResponse.equalsIgnoreCase("Fail")) {
                return -1;
            } else {
                return Integer.parseInt(returnResponse);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

}
public static final String START_URL = "http://10.0.2.2:8080/ThopWebService/rest/";
public static final String LOGIN_URL = START_URL + "user/login";
apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "diplomski.thop.thopmobileclient"
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.volley:volley:1.0.0'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.6.2'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
}

共有1个答案

吴弘壮
2023-03-14
   jackOptions {
     enabled true
   }
 类似资料:
  • 我正在尝试将图像添加到我新创建的应用程序中。但是,当我运行我的应用程序时,它会崩溃。我没有在我的应用程序中编写任何代码,因此我提供了XML代码和错误日志。我在以前的应用程序中使用了ImageViews。没有错误。我已经优化了我的图像,但它仍然给我错误。 图像详细信息 错误日志: 09-08 16:03:02.266 15137-15137/ooper.loopE/AndroidRuntime: F

  • 在以前的代码上,谁医生帮了我 现在,android应用程序在我的手机上运行时崩溃了,这是错误日志 第一个问题是在我用相机扫描二维码后,它不能显示在二维码的结果进入 第二个问题是,我从存储器中选择了一个QRcode图像,然后点击确认,它崩溃了 下面是我认为的问题 类型不匹配:推断的类型是Uri?但乌里是意料之中的 冗余SAM构造函数 'onRequestPermissionsResult(Int,数

  • 更新到QRCodeReaderView最新版本后,我的应用程序崩溃仍然没有停止,不同的问题得到。致命异常:java.lang.RuntimeException:无法连接到java:580)在android.view.choreographer.doFrame(choreographer.java:550)在android.view.choreographer$FrameDisplayEventRe

  • 我已经编写了一个混合应用程序,它使用react原生svg在react原生视图中呈现svg图像。一切都很完美。今天,当我为我的应用程序创建release apk并将其部署到模拟器时,它崩溃了,出现了一个错误: 只有当我从proguard规则中注释时,我才会看到这个问题。专业文件。我想混淆我的apk代码(我无法控制)。我使用的proguard文件与本例中显示的类似,https://github.com

  • 我开发了一个React Native应用程序,当应用程序处于活动状态时,每次收到来自OneSignal的消息推送时都会崩溃。 错误是。 如果我在应用程序处于后台时收到通知,则没有问题。 这是我的身材。gradle文件: 还有我的应用/构建。gradle文件: 你知道问题来自哪里吗?

  • 问题内容: 每次我从Eclipse导出签名应用程序并将apk文件安装到手机上时,该应用程序都会崩溃。这仅在启用Proguard的情况下发生。 这是我的保护文件 我运行了adb logcat命令来检查崩溃报告。以下是我认为很重要的报告摘要: 第二段: 如果有人可以帮助我,我将非常感谢并接受您的回答。 更新: 所以我将这些添加到我的proguard文件中 现在启用Proguard时出现与Jackson