无法解析getstring方法(java.lang.string)

问题描述:

我在使用2因素身份验证创建应用程序时遇到了一些麻烦。我决定使用twilio作为我的短信网关,并决定继续他们的教程。不过,当我声明一个URL发送短信的时候,我遇到了这个问题。出于某种原因,我不在乎有已经声明 “mContext”无法解析getstring方法(java.lang.string)

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import android.content.Context; 
import java.io.IOException; 
import okhttp3.Call; 
import okhttp3.Callback; 
import okhttp3.FormBody; 
import okhttp3.OkHttpClient; 
import okhttp3.Request; 
import okhttp3.RequestBody; 
import okhttp3.Response; 

public class MainActivity extends AppCompatActivity { 

private EditText mTo; 
private EditText mBody; 
private Button mSend; 
private OkHttpClient mClient = new OkHttpClient(); 
private Context mContext; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

mTo = (EditText) findViewById(R.id.txtNumber); 
mBody = (EditText) findViewById(R.id.txtMessage); 
mSend = (Button) findViewById(R.id.btnSend); 
mSend.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     try { 

    post(mContext.getString(Integer.parseInt("http://4a61510d.grok.io/sms")), 
new 
    Callback(){ 

       @Override 
       public void onFailure(Call call, IOException e) { 
        e.printStackTrace(); 
       } 

       @Override 
       public void onResponse(Call call, Response response) throws 
    IOException { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          mTo.setText(""); 
          mBody.setText(""); 
          Toast.makeText(getApplicationContext(),"SMS 
Sent!",Toast.LENGTH_SHORT).show(); 
         } 
        }); 
       } 
      }); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
}); 
mContext = getApplicationContext(); 
} 
Call post(String url, Callback callback) throws IOException { 
RequestBody formBody = new FormBody.Builder() 
     .add("To", mTo.getText().toString()) 
     .add("Body", mBody.getText().toString()) 
     .build(); 
Request request = new Request.Builder() 
     .url(url) 
     .post(formBody) 
     .build(); 
Call response = mClient.newCall(request); 
Response.**enqueue**(callback); 
return response; 
}} 

我的build.gradle以及

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
{ 
exclude group: 'com.android.support', module: 'support-annotations' 
}) 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:25.3.1' 
compile 'com.android.support.constraint:constraint-layout:1.0.2' 
compile 'com.squareup.okhttp3:okhttp:3.6.0' 

和我的清单XML

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

<application 
android:allowBackup="true" 
android:icon="@mipmap/ic_launcher" 
android:label="@string/app_name" 
android:roundIcon="@mipmap/ic_launcher_round" 
android:supportsRtl="true" 
android:theme="@style/AppTheme"> 
<activity android:name=".MainActivity"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 
</application> 

这是怎么得到一个错误我目前改变它仍然存在的错误

post(mContext.getString("http://4a61510d.ngrok.10/sms"), new Callback(){ 

尝试以下方法

post("http://4a61510d.grok.io/sms", new callback.... 

删除mContext.getString() & Integer.parseInt(..)方法。当你不使用任何资源

+0

仍然出现错误 – Wshivnarine

+0

这就是我现在的 – Wshivnarine

+0

@Wshivnarine删除mContext.getString我的朋友。检查我的答案 –

您有错误在这行

mContext.getString(Integer.parseInt("http://4a61510d.grok.io/sms") 

Integer.parseInt()仅用于在int分析的价值和你正在试图解析字符串转换成int

使用

mContext = this; 

in onCreate()方法。