Error in http connection java.net.SocketException: Permission denied
和
Error saving string null.pointer
任何帮助将不胜感激
public class MainActivity extends Activity {
String storyObj = "";
Object json = null;
HttpEntity entity = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//button that saves the file from mySQL
Button save = (Button) findViewById(R.id.downloadBtn);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveJson();
}
});
//Button that opens the file from InternalMemory
Button open = (Button) findViewById(R.id.showBtn);
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openJson();
}
});
//end of onCreate()
}
//saveJson pull a JSON file from mySQl server then saves that file in its JSON type eg .json
public void saveJson(){
TextView test = (TextView) findViewById(R.id.showView);
try{
//connects to mySQL
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.0.2.2/textures_story_list.php");
HttpResponse response = client.execute(post);
//captures the response
entity = response.getEntity();
}catch(Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
try{
Object json = entity;
String FILENAME = "story.json";
//gives file name
FileOutputStream output = openFileOutput(FILENAME, MODE_WORLD_READABLE);
//creates new StreamWriter
OutputStreamWriter writer = new OutputStreamWriter(output);
//writes json with file name story.json
writer.write((Integer) json);
writer.flush();
//closes writer
writer.close();
}catch(Exception e) {
Log.e("log_tag", "Error saving string "+e.toString());
}
//end of saveJson()
}
private char[] Object(Object json2) {
// TODO Auto-generated method stub
return null;
}
public void openJson(){
TextView test = (TextView) findViewById(R.id.showView);
try{
FileInputStream fileInput = openFileInput("story.json");
BufferedReader inputReader = new BufferedReader(new InputStreamReader(fileInput, "UTF-8"), 8);
StringBuilder strBuilder = new StringBuilder();
String line = null;
while ((line = inputReader.readLine()) != null) {
strBuilder.append(line + "\n");
}
fileInput.close();
storyObj = strBuilder.toString();
}catch(IOException e){
Log.e("log_tag", "Error building string "+e.toString());
}
try{
JSONArray jArray = new JSONArray(storyObj);
String storyNames = "";
for(int i = 0;i<jArray.length();i++){
storyNames += jArray.getJSONObject(i).getString("story_name") +"\n";
}
test.setText(storyNames);
}catch(JSONException e) {
Log.e("log_tag", "Error returning string "+e.toString());
}
return;
//and of openJson()
}
//end of class body
}
这是我的xml清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.game"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<users-permission
android:name="android.permission.INTERNET" />
<users-permission android:name="android.permission.SET_DEBUG_APP" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:3)
如果这确实是您的清单文件,则错误很明显。标记不应该是use[r]s-permission
,而是uses-permission
:
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
现在您无法访问互联网,从而导致错误。如果您切换到使用像Eclipse这样的IDE并使用自动完成功能,您将从一开始就避免此类错误,但IDE还会将此错误强调为警告。
编辑:关于你得到的错误 - 你试图将响应的实体直接转换为整数。这总是会失败,因为Integer不是HttpEntity
的超类。您需要在String
中读取实体的内容,然后将字符串的内容解析为整数:
InputStream entityStream = entity.getcontent();
StringBuilder entityStringBuilder = new StringBuilder();
byte [] buffer = new byte[1024];
int bytesReadCount;
while ((bytesReadCount = entityStream.read(buffer)) > 0) {
entityStringBuilder.append(new String(buffer, 0, bytesReadCount));
}
String entityString = entityStringBuilder.toString();
Integer responseInteger = Integer.valueOf(entityString);
这不是高度优化,而是完成工作。从那时起,您可以根据需要使用responseInteger
值。但是,如果您想writer.write
,则需要String
值,而不是Integer
。因此,我建议您使用entityString
。
答案 1 :(得分:1)
您插入了错误的权限。它必须是这样的
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
您使用过:
<users-permission ...
你在哪里得到String nullpointer?